admin管理员组文章数量:1389948
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"}}' \
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?
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/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?
Share asked Apr 25, 2017 at 14:21 fangiofangio 1,7965 gold badges29 silver badges56 bronze badges 3- create an object. – Junius L Commented Apr 25, 2017 at 14:25
-
in your ajax
data: {"title":"event", "description": "nice", "start": "2018-03-11T22:00:00.000Z"}
– Junius L Commented Apr 25, 2017 at 14:26 -
1
which in your case would be
data: {"title":title, "description": desc, "start": start}
assuming your title, description, and start variables are correct. – Jonas Grumann Commented Apr 25, 2017 at 14:27
2 Answers
Reset to default 3In 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);
});
Just use it like this if you're making a POST
$.post(request, {title: title, description: desc, start: start}, function (data) {
console.log(data);
});
本文标签: jQueryjavascriptAJAX POST json to rest apiStack Overflow
版权声明:本文标题:JQuery, Javascript, AJAX POST json to rest api - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744648813a2617561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论