2017-07-08 52 views
0

我想通過AJAX調用調用服務器方法。但是當我點擊按鈕並在那個時候調用AJAX函數時,它顯示錯誤。ReferenceError:AJAX調用服務器方法時未定義json

這裏是我的代碼

<input type="button" id="btn_findsubmit" value="Edit" class="button" /> 

$(document).on("click", "#btn_findsubmit", function (e) { 
    var c = $find("<%=cmbobx_search.ClientID %>"); 
    $.ajax({ 
     type: "POST", 
     url: "schoolregistration.aspx/GetSchoolName", 
     data: json.stringify({ schoolname: c.get_textboxcontrol().value }), 
     contenttype: "application/json; charset=utf-8", 
     datatype: "json", 
     success: OnSuccessGetSchoolName, 
     failure: function() { 
      alert("error! try again..."); 
     } 
    }); 
}); 

[的WebMethod] [ScriptMethod]

public static string GetSchoolName(string schoolName){ 
    //Here is the code 
} 

現在,當我點擊那個時候JavaScript的按鈕單擊事件工作,但AJAX方法的按鈕不調用服務器方法GetSchoolName(我知道通過執行調試模式)。

,並拋出一個錯誤:

ReferenceError: json is not defined

+0

'json.stringify'需要是'JSON.stringify' –

+1

你想'JSON.stringify()'大寫字母 – DelightedD0D

+0

我投票結束這個問題作爲題外話,因爲它看起來像一個印刷錯誤,或者是一個本地化的問題,如果沒有答案可能會被放棄。 – halfer

回答

3

應該JSON.stringify,不json.stringify

+0

最好作爲評論 – DelightedD0D

+0

@ DelightedD0D但是,如果這是一個答案? – bugwheels94

+0

@ bugwheels94這是「答案」,但這是簡單的印刷錯誤。一般來說,我們只是評論答案,並且因爲它沒有未來價值而關閉這個問題。 – DelightedD0D

0
<input type="button" id="btn_findsubmit" value="Edit" class="button" /> 

<script> 

    $(document).on("click", "#btn_findsubmit", function (e) { 

     $.ajax({ 
      type: "POST", 
      url: "Default.aspx/GetSchoolName", 
      data: JSON.stringify({ schoolName: "school name" }), 
      contentType: "application/json; charset=utf-8", 
      datatype: "json", 
      success: function (data) { 
       alert(data.d); 
      }, 
      failure: function() { 
       alert("error! try again..."); 
      } 
     }); 
    }); 

</script> 


[WebMethod] 
public static string GetSchoolName(string schoolName) 
    { 
    //Here is the code 
    return "success"; 
    } 

首先它必須是JSON.stringify沒有json.stringify,第二它必須是contentTypecontenttype第三[WebMethod]中的參數名稱必須與您的ajax數據中的參數名稱相同。 在這種情況下schoolName不是schoolname。 希望它能幫助你。

相關問題