Building on top of the question, How do I send a cross-domain POST request via JavaScript?, is there a way to create that request with a JSON body, instead of the name=value
params of the application/x-www-form-urlencoded
form?
Asked
Active
Viewed 395 times
0

Community
- 1
- 1

Yuriy Nemtsov
- 3,869
- 4
- 30
- 44
1 Answers
0
If you are familiar with jQuery, you can use something similar to this, and specifiy that the data is json through the contentType-option:
$.ajax({
url: "/your-taget.php",
contentType: "application/json",
data: "your JSON",
success: function(data){
// Do something here on success
}
});
Update:
Notice that if you are making a cross-domain request, and intend to return JSON from the server back to the client as well, then you need to send the data from the server to the client as JSONP - see this article for instance. Otherwise you will conflict with the browsers same-origin policy.

Christofer Eliasson
- 32,939
- 7
- 74
- 103
-
1Due to browser security limitations, cross-domain ajax requests are not allowed. Thus, the jQuery ajax method doesn't help in this case. Also, JSONP doesn't do a POST request and doesn't contain any of the data in request BODY. – rap1ds Nov 13 '11 at 11:28
-
You are right of course, my bad, what confused me about the cross-domain request was the crossDomain option of jQuery.ajax(). Can anyone explain the purpose of that option? – Christofer Eliasson Nov 13 '11 at 11:51