2013-07-17 68 views
0

我做了一個jquery ajax調用靜態page method。它工作正常,沒有任何參數。但如果我把參數,那麼它不會調用該頁面的方法。我有下面的代碼。JQqery ajax調用參數不工作

JAVASCRIPT

 $.ajax({ 
     type: 'POST', 
     url: 'ItemMaster.aspx/UploadFile', 
     contentType: 'application/json; charset=utf-8', 
     data: {'path':'mydata'}, 
     dataType: 'json', 
     success: function (msg) { 
      alert(msg.d); 
     } 
    }); 

PAGE方法

[WebMethod] 
    public static string UploadFile(string path) 
    { 
     return "Success"; 
    } 

是否有任何datatype不匹配的發生?我熱身谷歌一段時間沒有任何成功。請幫忙..

回答

2

您的數據對象需要是JSON字符串。嘗試

var dataToSend = JSON.stringify({'path':'mydata'}); 

$.ajax({ 
    type: 'POST', 
    url: 'ItemMaster.aspx/UploadFile', 
    contentType: 'application/json; charset=utf-8', 
    data: dataToSend, 
    dataType: 'json', 
    success: function (msg) { 
     alert(msg.d); 
    } 
}); 

如果您支持舊瀏覽器,請確保包含JSON.js。

+0

謝謝你的工作.. –

1

您要發送的數據不是json。要麼刪除內容類型,要麼將數據轉換爲json。

我想刪除內容類型。

$.ajax({ 
    type: 'POST', 
    url: 'ItemMaster.aspx/UploadFile', 
    data: {path:'mydata'}, // you may need to remove the quotes from path here 
    success: function (msg) { 
     alert(msg.d); 
    } 
}); 
+0

感謝您的回覆。我用這個。但它在警告框中給我'undefined'。此外,頁面方法的斷點不會命中。 –