This question already has answers here:
jQuery XML error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.'
(2 answers)
Closed 9 years ago.
I m try to write ajax get by using jquery in jsp file. But it always gave error message. I could`nt find the error in my method.
This is my ajax function. Please help me.
<script type="text/javascript">
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: '{"partyid":4}',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+data);
},
error:function(msg){
alert("failure");
}
});
</script>
Error is :
XML HttpRequest cannot load http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list.
No 'Access-Control-Allow-Orgin' header is present on the requested resource. Orgin 'http:// localhost:4040' is therefore not allowed access
Try your data
data: {"partyid":4},
in place of
data: '{"partyid":4}',
And try your success callback like,
success: function(msg){
alert("success : "+msg);// use msg not data
},
Full Code
$(function(){
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: {"partyid":4},
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+msg);
},
error:function(msg){
alert("failure");
}
});
});
You cloud app url works
Related
This question already has answers here:
jQuery - get AJAX response headers
(2 answers)
Closed 6 years ago.
Here i am calling one API, i am getting success response but they returning the total count header part , i don't know how to take the values.
See Here:
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(result) {
console.log(result);
},
error: function(errMsg) {
//console.log(errMsg);
}
});
Try this: request.getResponseHeader('x-Total-Count') in success callback
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, request){
alert(request.getResponseHeader('x-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});
the success callback has 3 params, you can Click here
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (result, textStatus, xhr) {
console.log(xhr.getResponseHeader('X-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});
I have a plain html page and I write some javascript code that use jquery ajax request. The ajax request sends to server successfully in firefox and IE, but does not work in google chrome! When the ajax request sent, chrome console shows net::ERR_INCOMPLETE_CHUNKED_ENCODING error. I don't know How can I solve this strange problem. Here is my ajax code:
var data = {};
data['type'] = "email";
data['value'] = "test#gmail.com"
$.ajax({
headers: {
Accept: "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
},
type: "POST",
url: "/testUrl",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(result) {
alert("success");
},
error: function(data) {
alert("error");
},
failure: function(errMsg) {
alert("failure");
}
});
I tested plain ajax request with XMLHttpRequest javascript object, but also has above problem.
With Chrome REST service I do this: a post to a certain url, I send a json string in the body, I set the content type as application/json and when I execute the post I get the proper answer.
I am trying to do the same post with jquery.
I first try with:
var beacon = {"beaconid.minor":2,"beaconid.UUID":"beaconnr","beaconid.major":1};
$.ajax({
type: "post",
data: JSON.stringify(beacon),
contentType: "application/json",
dataType: "jsonp",
crossDomain: true,
url: "myurl"}).done(function() {
alert("success");
}).fail(function()
{
alert("error");
});
by I seem to get no answer, I don't get the success alert nor the error alert.
I have then tried with:
var jqxhr = $.post( "myurl", {"beaconid.minor":2,"beaconid.UUID":"beaconnr","beaconid.major":1}).done(function(data, textStatus, jqXHR)
{
}).fail(function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
});
At least now I get an alert with the textStatus as an error. It is something...but not enough. I could I do a successful post?
The issue is you're setting dataType: "jsonp" for a POST request. JSONP doesn't support POST requests, only GET requests.
I suggest changing to dataType: "json" to see if the service supports CORS. If it doesn't then you'll have to do something like proxy the request through the local server, to get around CORS.
You should not be sending "post" as "type", rather as "method"
And like pointed above, the data type should be json and not jsonp.
$.ajax({
method: "post",
data: JSON.stringify(beacon),
contentType: "application/json",
dataType: "json",
url: "myurl"}).done(function(response) {
alert("success");
//if in chrome
//console.log(response);
}).fail(function(error)
{
//if using Chrome
//console.log(error);
alert(error);
});
I am facing a problem with the JQuery ajax function.
I am trying to send a simple json through the ajax asshown below.
$.ajax({
url: 'NewFile.jsp',
type: 'GET',
data: {"datasource":"hello1", definition:[{val1:"hello3"}]},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert("Successfully posted "+JSON.stringify(json));
}
});
The problem is that when I do
System.out.println(request.getParameter("datasource"));
System.out.println(request.getParameter("definition"));
in my NewFile.jsp then I get hello1 for the first and null for the second.
Why I get null value in the second println()?
Thanks
In the url variable inside your ajax object, give the full URL of your request. Like so:
$.ajax({
url: 'www.google.com/',
type: 'GET',
data: {"datasource":"hello1", definition:[{val1:"hello3"}]},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert("Successfully posted "+JSON.stringify(json));
}
});
Also, always make sure that there is a failure variable inside your object literal, so that you know it happened, but failed. Helps with debugging.
I get the following error when I try to get info from the mtgox API. In chrome I can see that using jsonp is working, and there's no cross domain errors:
$.ajax({
type: 'GET',
url: 'https://mtgox.com/api/1/BTCUSD/ticker?callback=?',
async: false,
contentType: 'application/json',
dataType: 'jsonp',
success: function(json) {
console.log('ok');
}
});