2014-02-24 52 views
0

我在asp.net中使用jquery ajax調用,我有一個靜態的web方法的一些參數,當我試圖調試它沒有擊中該方法,我在錯誤日誌中看到它的顯示parseError,我刪除了所有參數和檢查,但還是同樣的錯誤,在ASP.net jQuery Ajax調用錯誤

[WebMethod] 
    private static void AddData(int type, int categ, string desc, string date, string city, string state) 

     { 
//Do Processing 
     } 

我也試過這個,但同樣的錯誤

[WebMethod] 
     private static void AddData() 
     { 
//do Processing 
     } 

這是我的Ajax調用

$.ajax({ 
       type: "POST", 
       url: 'MyPage.aspx/AddData?type=' + encodeURIComponent(crimetype) + "&categ=" + encodeURIComponent(crimecateg) + "&desc=" + encodeURIComponent(desc) + "&date=" + encodeURIComponent(crimedate) + "&city=" + encodeURI(city) + "&state=" + encodeURIComponent(stateid), 

       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
       }, 
       error: function (data, errorThrown) { 
        debugger 
        alert(errorThrown); 
        alert(data.toString()); 
       } 
      }); 

我這個嘗試以及

$.ajax({ 
       type: "POST", 
       url: 'MyPage.aspx/AddData', 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
       }, 
       error: function (data, errorThrown) { 
        debugger 
        alert(errorThrown); 
        alert(data.toString()); 
       } 
      }); 

所有參數都傳遞正確

+0

並把網址中的**」「**,而不是**「**改變什麼? – Rohan

回答

0

所有參數將JSON數據,像這樣:

var jsonData = { 
    categ: value, 
    desc: value, 
    // and others 
} 

$.ajax({ 
       type: "POST", 
       url: 'MyPage.aspx/AddData' 
       data: jsonData //your parameters 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
       }, 
       error: function (data, errorThrown) { 
        debugger 
        alert(errorThrown); 
        alert(data.toString()); 
       } 
      }); 
+0

你能詳細解釋這個答案嗎?你做了什麼改變,影響是什麼? – Rohan

+0

當我調試方法 –

+0

忘記數據屬性在$ .ajax函數,它的屬性是包括所有你的url參數 – user1956570

1

更改訪問修飾符

private static void AddData() 

public static void AddData() 
+0

它沒有打到當我調試方法 –

+0

你的意思c#方法嗎?你在哪裏打電話給阿賈克斯? – iJade

+0

是它的C#web方法,我打電話給它在asp.net頁面 –

0

試試這個。

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
[WebMethod] 
public static void AddData() 
{ 
    //Your logic 
} 

而且在通話過程

$(document).ready(function() { 
      InsertData(); 
     }); 
     function InsertData() { 
      $.ajax({ 

       type: "POST", 

       contentType: "application/json; charset=utf-8", 

       url: "MyPage.aspx/AddData", 

       data: "{}", 

       dataType: "json", 
       success: function (response) {} 
       error: function (data, errorThrown) { 
        debugger 
        alert(errorThrown); 
        alert(data.toString()); 
       } 
      });