2013-08-22 47 views
0

我开始将事情切换到[WebMethod] s,但开始有麻烦,并决定我需要做一些正确使用它们的更多研究。在做一个简单的ajax调用时遇到了一些麻烦

我把$(document).ready()

   $.ajax({ 
       type: "POST", 
       data: "{}", 
       url: "http://localhost:3859/Default.aspx/ajxTest", 
       dataType: "json", 
       success: function (msg, status) { 
        alert(msg.name + " " + msg.value); 
       }, 
       error: function (xhr, status, error) { 
        var somethingDarkside; //only to put a breakpoint in. 
        alert(xhr.statusText); 

       } 
      }); 

[WebMethod]

我从来没有得到 “请工作” 以下。我得到“undefined undefined”我可以在VS中进行调试,并且调用进入[WebMethod],返回字符串对于JSON看起来是正确的,但我还没有能够成功调用它。我有解析错误,传输错误。一切都不一致,但没有任何事情是正确的。今天我已经有47个不同的博客,SO帖子和谷歌组合选项卡,但我无法完全破解它。

xhr.statusText状态即可。我得到status解析错误。我迷路了。

在此先感谢。

编辑:jQuery的1.9.1

EDIT2:DummyString对象

 public class DummyString 
    { 
     public string name { get; set; } 
     public string value { get; set; } 

     public DummyString(string n, string v) 
     { 
      name = n; 
      value = v; 
     } 
    } 

EDIT 3:我也有<asp:ScriptManager EnablePageMethods="true" ...

+0

究竟_are_你的结果字符串的内容? – Nuffin

+0

你有没有在浏览器中调试过它? –

+0

浏览器调试是我如何得到'status'解析错误。 'DummyString'就像一本字典。它返回一个'name:please,value:work'的东西。 – Bmo

回答

1

简化为:

$.ajax({ 
    type: "POST", 
    url: "Default.aspx/ajxTest", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     alert('success'); 
    }, 
    error: function (data) { 
     alert('fail'); 
    } 
}); 

[WebMethod] 
public static string ajxTest() 
{ 
    return @"{ ""hello"":""hola"" }"; 
} 

并测试。 (以上将工作)的.D参数

+0

我从此失败。这也是我开始的地方,尝试最简单的方法。我有一个糟糕的一天,这并不难。 – Bmo

+0

尝试GET。 (因为你不是真的发布) –

+0

也是一个失败。我将最终发布。我试图在那里建立我的路。这是一个双参数调用,当它是它自己的.aspx页面时工作得很好。 – Bmo

1

的服务回报响应尝试以下

msg.d.name 
+0

我正在研究@Sam Leach的方法。我已经从中得到了返回的数据。此时我无法完成呼叫。我阅读并尝试之前没有运气顺便说一句。 – Bmo

相关问题