English 中文(简体)
JSON请求中的错误
原标题:Error during JSON request

我要求,如果在登记程序中已经拿到用户名,就对服务器进行检查。

这就是说:

// Instant check availability of the username
$("#txtUserName").blur(function() {
    if ($("#txtUserName").val() == "") return;
    $.ajax({
        type:  post ,
        url:  /Login/CheckUserName ,
        data: "{userName:  " + $("#txtUserName").val() + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(message) {
            //Set the spanChecking text letting user know if the uname is available
            if (message.d == true) {
                $("#userNameCheck").css({ "color": "red", "font-weight": "bold", "font-size": "small", "padding-left": "5px" });
                $("#userNameCheck").text("Non disponibile");
            }
            else {
                $("#userNameCheck").css({ "color": "green", "font-weight": "bold", "font-size": "small", "padding-left": "5px" });
                $("#userNameCheck").text("Disponibile");
            }
        },
        error: function(errormessage) {
            //this is just to see if everything is working. Remove before deploy
            $j("#userNameCheck").text(errormessage.responseText);
        }
    });
});

这也是控制者为请求服务的行动。

[HttpPost]
public JsonResult CheckUserName( string userName ) {
    if ( Membership.GetUser( userName ) == null )
        return Json(false);
    else
        return Json(true);
}

不管怎么说,我会理解为何我从服务器上错了500个。 看一下菲德勒,我可以看到,妇女联盟的要求是:

POST http://localhost:1037/Login/CheckUserName HTTP/1.1
Host: localhost:1037
Connection: keep-alive
Referer: http://localhost:1037/Login/Register
Content-Length: 22
Origin: http://localhost:1037
X-Requested-With: XMLHttpRequest
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{userName:  sampleuser }

但是,控制器行动为用户Name收取一个无效参数。 我通过菲德勒看到这一点,甚至对法典设置了一个空白点。

我在哪里做错?

http://www.ohchr.org。

退还客户的错误是

The value cannot be null. Parameter name: username

请注意,错误中的参数名称并不保留原案件。 这是否是会员提供人的行为,或者我是否应当降低参数的情况?

最佳回答

你是作为JSON提出这一请求的,但服务器中没有任何能够理解或期望这种形式。 你们有控制器行动,希望<代码> 申请/x-www-form-urlencoded内容类型。 相反:

data: { userName: $( #txtUserName ).val() },
contentType:  application/x-www-form-urlencoded ,

这还有利于妥善保存发给服务器的参数,在您的版本中,使用严格分类实现了这些参数。

问题回答

这是:

{userName:  sampleuser }

......不适用。 这是而且可能是你想要的:

{"userName": "sampleuser"}

See the RFC, for the details of the JSON Format.

用这一格式处理<代码>数据参数,请替换:

data: "userName=" + $("#txtUserName").val()

你们是否检查了路线,特别是资本化?

在你发出这一呼吁之后,也许会在那里有点?





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签