2011-10-02 67 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