简体   繁体   English

带MVC2的ASP.NET AJAX

[英]Asp.net ajax with mvc2

I am calling a ajax method as below 我正在调用如下的ajax方法

 var srchText = "Chicago";


 $.ajax({
    url: "/Ajax/GetCities",
    data: "{'srchText' : '" + srchText + "'}",
    dataType: "json",
    type: "POST",
    async: false,
    contentType: "application/json; charset=utf-8",
    dataFilter: function (data) { return data; },
    success: function (data) {
        cityList = data.d;
    }
});

The url is pointing to a MVC controller, as below, 网址指向一个MVC控制器,如下所示,

 [HttpPost]
    public ActionResult GetCities(string srchText)
    {
        List<City> result = new List<City>();
        EventsBIZ objBIZ = new EventsBIZ();
        result = objBIZ.ToList<City>(objBIZ.GetCities(srchText));
        return this.Json(new GetEventsResponse() { d = result }, JsonRequestBehavior.AllowGet);
    }

There is something wrong with the code, that the method is called successfully, but the srchText is coming as null. 该代码有问题,该方法已成功调用,但srchText为null。 Please help me to figure out wat went wrong. 请帮助我找出错误的wat。 Thanks in advance 提前致谢

Adding the request tracked from firebug. 添加从Firebug跟踪的请求。 FireBug Net监视器

The reason your code doesn't work is because by default ASP.NET MVC 2 doesn't understand JSON requests. 您的代码不起作用的原因是,默认情况下,ASP.NET MVC 2无法理解 JSON请求。 There is nothing built-in that allows you to send a JSON formatted request and that this request is parsed back to a strongly typed action argument. 没有内置的组件可以让您发送JSON格式的请求,并且可以将该请求解析回强类型的操作参数。 This functionality is built-in by default starting from ASP.NET MVC 3. Take a look at the following blog post . 从ASP.NET MVC 3开始,默认情况下内置此功能。请查看以下博客文章 You will need to implement a JsonValueProviderFactory if you want to make this work under ASP.NET MVC 2. 如果要在ASP.NET MVC 2下进行此工作,则需要实现JsonValueProviderFactory

Also instead of: 也可以代替:

data: "{'srchText' : '" + srchText + "'}",

you should use: 您应该使用:

data: JSON.stringify({ srchText: srchText }),

The JSON.stringify is native for modern browsers, and for older you might need to include json2.js . JSON.stringify是现代浏览器的本机,对于较旧的浏览器,您可能需要包括json2.js

Another possibility if you don't want to implement a JsonValueProviderFactory is to use a standard application/x-www-form-urlencoded request which the default model binder can understand: 如果您不想实现JsonValueProviderFactory则另一种可能性是使用默认模型绑定程序可以理解的标准application/x-www-form-urlencoded请求:

$.ajax({
    url: '/Ajax/GetCities',
    data: { srchText: srchText },
    type: 'POST',
    async: false,
    dataType: 'json',
    dataFilter: function (data) { return data; },
    success: function (data) {
        cityList = data.d;
    }
});

you don't have to send srchText as json becuase you will just send a string so can send it as query string 您不必将srchText作为json发送,因为您只需发送一个字符串即可将其作为查询字符串发送

try this 尝试这个

 var srchText = "Chicago";


$.ajax({
url: "/Ajax/GetCities",
data: 'srchText=' + srchText ,
type: "POST",
async: false,
dataFilter: function (data) { return data; },
success: function (data) {
    cityList = data.d;
}
});

我认为您没有传递有效的JSON,请尝试使用:

data: {"srchText" : srchText},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM