sulla base di questo tutorial: JSON Web Service And jQuery with Visual Studio 2008
Il metodo Web restituisce un prodotto che è serialize d in formato JSON. Poiché non esiste il tipo JSON
, il valore restituito è un String
con il formato JSON.
Sul lato client, la chiamata ajax restituisce un JSON.
Il risultato assomiglia {d: 'returned-string-with-JSON-format'}
Più esattamente qualcosa di simile: {d:'{"ID":123,"Name":"Surface Pro 2"}'}
Nota che 'returned-string-with-JSON-format'
è un non stringa di un oggetto JSON in modo da non può fare result.d.ID
.
Invece è necessario convertirlo in oggetto JSON utilizzando JSON.parse(result.d)
o eval(result.d)
Alla fine, ciò che si vuole veramente è fare questo:
result = JSON.parse(result.d)
UPDATE Considera anche questa demo, dove uso un JSON in formato stringa e lo converto in oggetto JSON:
correlati: http: // stackov erflow.com/questions/739859/returning-html-from-json-webservice-what-is-the-d –
related: http://stackoverflow.com/questions/2811525/removing-the-d-object-from- asp-net-web-service-json-output NB Ho cambiato la mia opinione sul mio VTC su questa domanda - questa domanda nonostante sia più recente è più focalizzata e ha risposte migliori –