2013-12-11 102 views
-1

我有這樣的JSON:發送嵌套的Json到Web服務

var datatosend = 
{ 
    moduleID: '1', 
    data: { SomeData: 'SomeValue' } 
}; 

並將其與此發送到Web服務:使用此功能

$.ajax({ 
     type: "POST", 
     async: false, 
     url: SomeWebService, 
     data: datatosend, 
     dataType: 'json', 
     contentType: "application/json; charset=utf-8", 
     success: function (msg) { 
      elm.append(msg.d); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert(xhr.responseText); 
      alert(thrownError); 
     } 
    }); 

我剛剛得到一個錯誤:

[WebMethod(EnableSession = true)] 
public string SomeFunction(int moduleID, object data) { 
      //do something 
} 

任何解決方案?

+0

你什麼錯誤? –

+0

@NilsMagneLunde用這段代碼我得到無效的json原語:moduleID ... – Raika

回答

1

在服務器上:

public string SomeFunction(int moduleId, string data) 
{ 
     //do something 
} 

在客戶端,嘗試透過JSON.stringify你要發送的對象:

JSON.stringify(datatosend) 
+0

gettin同樣的錯誤。甚至將datatosend值更改爲精確的大寫字母。 moluleID => ModuleId – Raika

+0

是$ .parseJSON做同樣的事情? – Raika

+1

不,這是完全相反的 –