Reputation: 1786
I have a problem with posting data to a REST API, it should be done like this:
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" \
-d '{"event":{"title":"event", "description": "nice", "start": "2018-03-11T22:00:00.000Z"}}' \
http://events.restdesc.org/events
I have the following code:
function eventedit(request){
console.log(request);
var title = $("#title").val();
var desc = $("#desc").val();
var start = $("#start").val();
start += ".000Z";
$.ajax({
url: request,
type: "POST",
dataType:'json',
success: function (response) {
console.log(response);
},
error: function(error){
console.log("Something went wrong", error);
}
});
}
Like you see, I need to add data in my ajax request, but I don't know how to do it, do I need to make a string containing those values? Or an array?
Upvotes: 1
Views: 12438
Reputation: 16122
In your $.ajax
call add data
.
$.ajax({
url: request,
type: "POST",
data: {"event":{"title": title, "description": desc, "start": start}},
dataType:'json',
success: function (response) {
console.log(response);
},
error: function(error){
console.log("Something went wrong", error);
}
});
for POST
you can also use a shorthand $.post
$.post(request, {"event":{"title": title, "description": desc, "start": start}}, function(data){
console.log(data);
});
Upvotes: 3
Reputation: 450
Just use it like this if you're making a POST
$.post(request, {title: title, description: desc, start: start}, function (data) {
console.log(data);
});
Upvotes: 0