2012-07-30 98 views
1

我一直在用以下C#/ jQuery获得500。任何给定的实现可能都不对,这不是一个大问题。我只是试图让一个你好的世界了。它的工作原理如果C#没有参数,但只要我尝试收到的数据也给出了500ASP.NET hello world AJAX post

[WebMethod] 
public static string Test(string s) 
{ 
    // never gets here 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/" + method, 
    /*async: true,*/ 
    data: "{data:'" + data + "'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     callback(data.d); 
    } 
}); 

最新尝试是这仍然不工作:

[WebMethod()] 
public static string Test(string data) 
{ 
    // never gets here 
    return "hello world"; 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/Test", 
    data: "data: {data:'abc'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     alert("back"); 
    } 
}); 
+0

如何设置'data'?在'data:“{data:'”+ data +“'}”' – 2012-07-30 00:14:20

+0

数据变量是一个没有引号的字符串。即数据是 - > abcdefg – 2012-07-30 00:16:28

+0

您正在使用什么URL来尝试访问该控制器方法? – 2012-07-30 03:51:50

回答

1

我想你不必使用MVC来使它工作。我认为你传递JSON参数的方式是错误的。请检查下面的代码,并尝试让我知道它是否有效。

[WebMethod()] 
public static string Test(string data) 
{ 
    // never gets here 
    return "hello world"; 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/Test", 
    data:'{"data":"abc"}', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     alert(response); 
    } 
}); 
+0

最后让它使用它来工作。 [WebMethod]没有括号。 – 2012-07-31 12:59:12

0

试试这个

[HttpPost] 
public ActionResult Test(string x) 
{ 
    // never gets here 
    return Json(true) 
} 

$.ajax({ 
    type: "Post", 
    url: "ajax/Test", 
    data: {x:'abc'}, 
    dataType: "json", 
    success: function (data) { 
     alert("back"); 
    } 
}); 
+0

仍然不能正常工作: – 2012-07-30 00:27:53

+0

不确定,但更改参数名称,使其成为'data:{s:“abc”}' – 2012-07-30 00:32:00

+0

加载资源失败:服务器响应状态为500(内部服务器错误)http:// localhost:16679/ajax.aspx /测试 – 2012-07-30 00:38:41