2011-10-02 66 views
0

我有以下的WebMethod在我的C#/。NET网络的应用程序,文件lite_host.aspx.cs:AJAX调用一个WebMethod

[WebMethod] 
public static bool UpdatePage(string accessCode, string newURL) 
{ 
    bool result = true; 
    try { 
     HttpContext.Current.Cache[accessCode] = newURL; 
    } 
    catch { 
     result = false; 
    } 

    return result; 
} 

应该得到 “accessCode” 和 “的newURL” 的价值从通过jQuery的AJAX调用一个JavaScript函数,并在高速缓存适当的修改:

function sendUpdate() { 
     var code = jsGetQueryString("code"); 
     var url = $("#url_field").val(); 
     var options = { error: function(msg) { alert(msg.d); }, 
         type: "POST", url: "lite_host.aspx/UpdatePage", 
         data: {'accessCode': code, 'newURL': url}, 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         async: false, 
         success: function(response) { var results = response.d; } }; 
     $.ajax(options); 
} 

然而,当我打电话sendUpdate()函数,我在我的剧本上$就错误结束,我得到一个警告文字“未定义”。

回答

0

未定义表示msg.d不存在。尝试使用console.log(msg)并使用Chrome的调试器查看输出内容(是的,我知道)到控制台。

+0

它打印“500(内部服务器错误)”。但我不确定是什么原因导致了这个错误。 – cycero

+0

在你的错误函数中,添加另一行:console.log(data)并确保它的格式符合你的期望。 – Joe