2012-12-07 24 views
0

我在.net1.1 onload body.My webmethod調用此javascript方法「測試」返回字符串數據,但我無法獲取該數據我的JQuery方法。 在HiddenPage.aspx ==============================JQuery方法控制去成功塊,但msg.d顯示未定義

功能測試() {

 debugger; 

$.ajax({ 
       type: "POST", 
       url: "HiddenPage.aspx/GetServerTime", 
       //async : false, 
       //data: "i=1", 
       contentType: "application/json", 
       //dataType: "text", 
       success: function(msg) 
       // error: function(, textStatus, errorThrown) { 
       { 
        debugger; 
         alert(msg.d); 

       }, 
       error: function(msg) 
       //complete: function (jqXHR, textStatus) { 

       { 
        debugger; 
        alert(msg.d); 
        alert("Error! Try again..."); 
        //return false; 
       } 


      }) 
    // return ''; 
    } 

在HiddenPage.aspx.cs我已經把webmthod.My的WebMethod是: -

[WebMethod()] 
    public static string GetServerTime() 
    { 

     return DateTime.Now.ToString(); 
    } 
+0

後GetServerTime() – giftcv

+0

的代碼@ giftcv,我有added.please檢查我的編輯code..thanks – F11

回答

0

不太確定自己的返回數據的樣子,但你可以嘗試以下。

$.ajax({ 
    type: "POST", 
    url: "HiddenPage.aspx/GetServerTime", 
    //async : false, 
    //data: "i=1", 
    contentType: "application/json", 
    dataType: "html", 
    success: function(data){ 
     alert(data); 
    }, 
    error: function(jqXHR, textStatus) { 
     debugger; 
     if (jqXHR.status === 0) alert('Not connect.\n Verify Network.'); 
     else if (jqXHR.status == 404) alert('Requested page not found. [404]'); 
     else if (jqXHR.status == 500) alert('Internal Server Error [500].'); 
     else if (textStatus === 'parsererror') alert('Requested JSON parse failed.'); 
     else if (textStatus === 'timeout') alert('Time out error.'); 
     else if (textStatus === 'abort') alert('Ajax request aborted.'); 
     else alert('Uncaught Error.\n' + jqXHR.responseText); 
     //return false; 
    } 
    //return ''; 
}​ 
+0

返回的數據只是一個字符串,請查看我編輯的問題,控制去成功塊..謝謝 – F11

1

能否請您發表您的返回數據的代碼。

我建議你創建一個ASMX文件來使用web服務。它很容易使用。 創建一個webservice,然後確保你的webmethod之前在你的webservice中添加了以下行。

[System.Web.Script.Services.ScriptService] 

之後,你可以添加你的Web方法相同,你寫。

你的jQuery應該是這樣的。

$.ajax({ 
      type: "POST", 
      url: "webservice/WebService1.asmx/GetServerTime", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccessCall, 
      error: OnErrorCall 
     });   

    function OnSuccessCall(msg) { 

     alert(msg.d); 


    } 

    function OnErrorCall(msg) { 
     alert(msg.status + " " + msg.statusText); 
    } 

它可以幫助你。快樂的編碼。

+0

@plase檢查我編輯的問題..謝謝 – F11

0

嘗試以下

$.ajax({ 
    type: "POST", 
    url: "HiddenPage.aspx/GetServerTime", 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
      alert(msg.d); 
    // Do something interesting here. 
    } 
}); 
相關問題