我的jquery ajax请求有问题。我在函数触发时遇到此错误
{"消息":"对象引用未设置为对象的实例。"," StackTrace":" at Test.Web.Ajax.Test(String var1,String var2,Int32 var3,Int32 var4,String var5,String var6,String fvar7,String var8,String var9,String var10,String var11,String var12,Boolean var13)&# 34;," ExceptionType":" System.NullReferenceException"}
我的帖子JSON在下面给出
{"var1":"2","var2":"1","var3":0,"var4":20,"var5":"","var6":"","var7":"0","var8":"","var9":"","var10":"","var11":"","var12":"","var13":true}
客户端功能
function test(var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12,var13){
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data: JSON.stringify({ var1: var1, var2: var2, var3: var3, var4: var4, var5: var5, var6: var6, var7: var7, var8:var8, var9: var9, var10: var10, var11: var11, var12: var12, var13: var13}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
//more stuff
}
});
}
服务器端功能
[WebMethod]
public static ArrayList Test(string var1,string var2, int var3, int var4, string var5, string var6, string var7, string var8, string var9, string var10, string var11,string var12, bool var13)
{
//more stuff
}
我的json post对象不是null但我仍然收到此错误我不知道如何解决这个问题..请帮帮我
答案 0 :(得分:1)
请检查代码中是否存在类似异常的情况(Nullreferenceexception),因为您在WebMethod中使用空字符串
如果这是Jquery Ajax帖子的问题,您可能会收到invalidacastexception或内部服务器错误而不是空引用异常。
无论如何,我在下面包含了ajax调用的脚本。
<script type="text/javascript">
$(document).ready(function () {
test(1, 2, 3, 4, 5, 6, 7, 8, 9, 99, 999, true);
});
function test(var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12) {
$.ajax({
type: "POST",
url: "Default.aspx/Test",
data: JSON.stringify({ var1: var1, var2: var2, var3: var3, var4: var4, var5: var5, var6: var6, var7: var7, var8: var8, var9: var9, var10: var10, var11: var11, var12: var12 }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert('success');
//more stuff
},
error: function (xhr) {
alert('error');
alert(xhr.responseText);
}
});
}
</script>
希望这有帮助。