Ich versuche, eine JSON-basierte Web-API in einer Sinatra-App zu schreiben. Ich möchte ein JSON-Objekt als Post-Body POST (mit dem richtigen Content-Typ festgelegt), aber ich habe Probleme.Einstellen der POST-Körper zu einem JSON-Objekt mit jQuery
In Cocoa, würde ich etwas tun, wie
[mutableHTTPRequest setHTTPBody:dataRepresentationOfJSONObject];
Und den Inhaltstyp, auf JSON, würde dann den HTTP-Körper als JSON-Objekt erstellen. Ich versuche das mit jquery zu machen. Das Beste, was ich bisher tun kann, nimmt nur das JSON-Objekt und verwandelt es in einen normalen Stil key=value&…
Stil Post-Körper, und das ist nicht was ich bin.
Mein Javascript:
var data = { "user" : "me!" };
$.ajax({
type: "POST",
url: "/api/user/create",
contentType: 'application/json',
data: data,
success: function(r) {
});
Irgendwelche Hinweise, wie dies zu tun? Mein Ziel ist es für meine Sinatra wie folgt
post "/api/user/create" do
js = JSON.parse(request.body.read)
# do something with the js object… this works when POSTing from Cocoa
end
Hinweis: Prozessdaten sind optional. –