Asp.net 只是另一个jqueryajax没有正确发布参数
我在谷歌上搜索过很多类似的情况,但没有一个能解决我的问题。请看一下我的代码: JavaScript:Asp.net 只是另一个jqueryajax没有正确发布参数,asp.net,ajax,jquery,get,Asp.net,Ajax,Jquery,Get,我在谷歌上搜索过很多类似的情况,但没有一个能解决我的问题。请看一下我的代码: JavaScript: $.ajax({ type: 'POST', url: 'alarmInfo.aspx', data: {request:'BasicGpaInfo'}, dataType: "json", success: function (data) { alert(data);
$.ajax({
type: 'POST',
url: 'alarmInfo.aspx',
data: {request:'BasicGpaInfo'},
dataType: "json",
success: function (data) {
alert(data);
},
error: function () {
alert("Error in loading alarm information!");
}
});
ASP.NET:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["request"] == "BasicGpaInfo")
{
Response.Write(BasicGpaInfo());
}
else
{
Response.Write("Nothing");
}
}
这总是返回“Nothing”,断点告诉请求。表单为空。我尝试了GET
和Request.QueryString
,它们给出了相同的情况
我猜ajax函数中的数据
有问题,我尝试了以下方法,但都没有用:
data: $.param({request:'BasicGpaInfo'})
data: "{request:'BasicGpaInfo'}"
data: {request:'BasicGpaInfo'}
它不能在所有的网络浏览器上运行
请给我一些建议。谢谢 在发布之前尝试使用JSON.stringify。对于在IE中使用JSON.stringify,我测试了您的代码,并且运行良好。但是,它总是返回“加载报警信息时出错!”,因为您没有从服务器返回Json
Javascript很好,一旦返回json,它就会成功
返回整个页面,ajax方法从BasicGpaInfo()获取整个html而不是Json
尝试在警报中设置一个断点,您将看到所有数据都包含在数据中
error: function (data) {
alert("Error in loading alarm information!");
}
或者试试
error: function (data) {
alert(data);
}
这是完整的代码
$.ajax({
type: 'POST',
url: 'Default.aspx/BasicGpaInfoWebMethod',
data: { request: 'BasicGpaInfo' },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error in loading alarm information!");
//alert(data); // uncomment to see the whole response
}
});
您的webmethod将是:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string BasicGpaInfoWebMethod(string request)
{
return request;
}
我相当肯定问题不在于javascript——那里一切都井然有序。只需使用普通javascript对象,无需$.param
。你在使用Firefox吗?如果是,你有Firebug吗?您可以使用Firebug或Chrome的内置控制台检查发送的post数据。在Chrome中点击F12,在Firefox中点击insteall Firebug,然后点击F12。Firefox或Chrome中没有发布任何参数
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string BasicGpaInfoWebMethod(string request)
{
return request;
}