Je ne peux pas retourner javascript objet à la méthode web à travers appel ajax comme ci-dessous:objet De retour à la méthode de service Web ajax
var person = {"FirstName":"Foo","LastName":"Bar"};
$.ajax({
type: "POST",
url: url,
data: person,
contentType: "application/json; charset=utf-8",
dataType: "json",
onSuccess: function() { alert('Success!'); },
onError: function() { alert('Error'); }
});
est non valide JSON primitive jeté avec la méthode ci-dessus. je voudrais de recourir à la manipulation de chaînes comme ceci:
var person = {"FirstName":"Foo","LastName":"Bar"};
var json = '{person:' + JSON.stringify(person) + '}';
$.ajax({
type: "POST",
url: url,
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
onSuccess: function() { alert('Success!'); },
onError: function() { alert('Error'); }
});
Maintenant, cela fonctionne très bien, je peux sortir avec cette solution, mais la vie est difficile, nous ne voulons pas plus difficile! S'il y a un moyen de passer l'objet que j'aimerais directement l'entendre;)
Quel type d'erreur avez-vous? D'abord, un exemple fonctionne bien. – Oyeme
@Oyeme: il lance "JSON primitif invalide: FirstName blahh" .. Il a besoin d'un wrapper Person pour reconstruire l'objet, d'où la manipulation de la chaîne .. Cela a fonctionné sur votre machine ?? – Kagawa
Oui, cela fonctionne. Vous avez corrigé le format JSON {"FirstName": "Foo", "LastName": "Bar"}; – Oyeme