J'essaie de faire un appel Ajax à une page aspx Sur la demande côté serveur vient comme "objet objet" et je ne peux pas le sérialiser en obtenant "pas une primitive JSON .." il fonctionne cependant quand au lieu de l'objet de json je passe la chaîne de json .... Le problème est que du côté du client j'emploie un objet de json que je dois convertir en chaîne avant l'envoi. J'ai essayé d'utiliser la fonction JSONToString() de json.org mais cela jette une erreur quand j'ajoute jquery library. Est-ce que quelqu'un sait comment cela devrait être fait? J'apprécierais beaucoup d'aide.Ajax appel à la page aspx JQuery JSON
add "JQuery/jquery-1.3.2.js"
ajouter "js/json.js"
<script type="text/javascript">
function callAjax() {
var myjson = { document: { manufacture: { item: ['Alfa Romeo']}} }
$.ajax({
url: 'jsonresponse.aspx',
type: 'POST',
//contentType: "application/json; charset=utf-8",
data: myjson.toJSONString(), // throws an error in json libary // return JSON.parse(this, filter); //.. Microsoft JScript compilation error: Syntax error
//data: myjson, can't serialize on the server request comes as object object
//data:{ document: { manufacture: { item: ['Alfa Romeo']}} }, works but I need something to convert object to a string as it is much bigger then the one in example
timeout: 1000000,
dataType: "json",
error: function() {
alert("error");
},
success: function(myResult) {
//alert(myResult);
}
});
}
jsonresponse.aspx
XmlDocument myxml = new XmlDocument();
StreamReader reader = new StreamReader(Page.Request.InputStream);
string test;
test = reader.ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
myxml = jss.Deserialize<XmlDocument>(test);
Fonctionne parfaitement !!! Merci beaucoup! –
Excellent, content que ça marche. –