I have a problem with JSON query.
I research on the forum and a lot of other websites to find how to fix the problem, but I used probably all of the options to repair my script...
At the moment my script:
jQuery.ajax({
url: "http://target.xyz/?f=json",
dataType: 'JSONP',
jsonpCallback: 'callback',
type: 'GET',
success: function (data) {
console.log(data);
}
});
Before the script, I use normal JSON but I saw:
XMLHttpRequest cannot load http://target.xyz/?f=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mypage.xyz' is therefore not allowed access.
When I use JSONP on my target website, I get data but website returned:
one error Uncaught SyntaxError: Unexpected token :
Response data:
{
"date" : "2017-05-29",
"gold" : "1267.00",
"silver" : "17.39",
"platinum" : "956.05"
}
Please give me some advice or links. Thanks.
Related
I am attempting to loop through the JSON file and append each instance of "sliderLink" to a preexisting DIV, however it appears it is not working.
I am getting the following error code in the console:
Access to XMLHttpRequest at 'http://www.coopertimewell.com/mainSlider.json' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
How do I fix this?
But this seems to be working in jsbin
<div class="custom-container">
</div>
<script>
//populate timeline select menu
$(document).ready(function() {
$.ajax({
url: 'http://www.coopertimewell.com/mainSlider.json',
type: 'GET',
dataType: "json",
crossorigin: true,
success: fillInFields
});
});
function fillInFields(data) {
var pictureURLArray = [];
$.each(data, function(index, value) {
pictureURLArray.push(value.sliderLink);
});
var lengthDatabase = Object.keys(data).length;
for (i = 0; i < lengthDatabase; i++) {
$(".custom-container").append(pictureURLArray[i]);
}
};
</script>
You should check console of this request (F12 and choose Console tab)
$.ajax({
url: 'http://www.coopertimewell.com/mainSlider.json',
type: 'GET',
dataType: "json",
crossorigin: true,
success: fillInFields
});
if have error like as: Access to XMLHttpRequest at 'http://www.coopertimewell.com/mainSlider.json' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
You should install Moesif Origin & CORS Changer extension for Chrome and setting like as below:
[]
After that turn on this extension and try it.
This is a question about how to support CORS. You should read the questions like this:Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?
Anyway, the simpliest fix for you is to change dataType: "json" to "jsonp".
So it turns out the actual URL was causing the issue.
Only change that I had to make:
$(document).ready(function() {
$.ajax({
url: 'mainSlider.json',
type: 'GET',
dataType: "json",
crossorigin: true,
success: fillInFields
});
});
I made this function to request some tickets from JIRA,I gave the data type as jsonp to avoid the Cross origin problem , and when I make the request I get the response in browser debugger , but cannot handle the json response "SyntaxError: missing ; before statement" , is there any way to read json if I send the request for jsonp ?
var ajaxUrl = jira/rest/api...
jQuery.ajax({
url:ajaxUrl,
dataType: 'jsonp',
jsonpCallback: 'callback',
type : "GET",
beforeSend: function (xhr){
success: function w(data){
console.log(data);
alert('Sucess data: ' + data);
};
error: function e(data){alert('alert error');}
}
})
I solved the problem , I changed the jsonp in json , and I remade the corss from jira server how it is explained here : https://community.atlassian.com/t5/Answers-Developer-Questions/Enable-CORS-in-JIRA-REST-API/qaq-p/553997 , it looks like the problem was on the preflight part.
I am using Phonegap to connect to a server to authenticate a user. The server expects the data to be Json encoded, and I will get the response back from the server.
var serviceURL = "http://appdev/PEPS-CS-Services/";
The serviceURL (APPDEV) is hosted on our network within the office.
var loginData = {
'strUniqueID' : '123',
'UserName' : 'User123',
'Password' : 'Password123'
};
I have checked the login credentials and they are fine. When I try the following:
$.ajax({
type: "POST",
url: serviceURL + "services/AuthenticationServices/authenticateUser",
data: loginData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
},
failure: function(errMsg) {
alert(errMsg);
}
});
I get the Access-Control-Allow-Origin error in my console.
XMLHttpRequest cannot load http://appdev/PEPS-CS-Services/services/AuthenticationServices/authenticateUser.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access.
I had a look around, and I was suggested to use Jsonp as the dataType in the ajax request, which does give me back the response I was looking for. However, it isn't alerted out as it should be, the console shows the url with the parameters used in the loginData variable, as below
Resource interpreted as Script but transferred with MIME type application/xml:
http://appdev/PEPS-CS-Services/services/AuthenticationServices/authenticateUser?callback=jQuery164017807046906091273_1396515434941&strUniqueID=123&UserName=Mark&Password=Password1&_=1396515434963"
When I open the link in a browser i get the correct response, as below
<ns:authenticateUserResponse xmlns:ns="http://services">
<ns:return>SESSION ID HERE</ns:return>
</ns:authenticateUserResponse>
However, I also get the Uncaught SyntaxError: Unexpected token < error below it.
I have tried to change to data: loginData to data: {'data':$.toJSON(loginData)}, but still the same error.
My questions are the following:
Am I sending the data over correctly? Why does the jQuery164017807046906091273_1396515434941 get added to the URL before the parameters?
And why am I getting the Uncaught SyntaxError too?
Is this because the server is sending back the incorrect format of the data? It is currently sending back XML
Any help would be greatly appreciated, thanks
This is a familiar cross context issue, you are not allowed to request resource from another domain with simple ajax call and expect a result.
Instead, use a jsonp call and regiter a callback function to call when return result:
var success = function(data){
/* parse JSON */
data = $.parseJSON(data);
/* show json parsed data */
alert(data);
};
$.ajax({
url: serviceURL + "services/AuthenticationServices/authenticateUser",
dataType: 'jsonp', //use jsonp data type in order to perform cross domain ajax
crossDomain: true,
data: loginData,
success: success,
error: function(errMsg) {
alert(errMsg);
}
});
I'm trying to get a bitcoin address info using the Blockchain.info API but it doesn't work. I get allways the same error:
XMLHttpRequest cannot load https://blockchain.info/address/13DKdDeZcdLbGoNquWz8nHBnVpfBVkDitz?format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:57366' is therefore not allowed access.
My code is:
$.getJSON("https://blockchain.info/address/13DKdDeZcdLbGoNquWz8nHBnVpfBVkDitz?format=json",
function(data) {$('#blockchain').append(JSON.stringify(data));
});
I've tryed the same code with another API and it works:
$.getJSON("http://btc.blockr.io/api/v1/address/txs/13DKdDeZcdLbGoNquWz8nHBnVpfBVkDitz",
function(data) {$('#blockr').append(JSON.stringify(data));
});
The problem is that I need some information that is only available by the blockchain API.
Any idea?
Try to use dataType: 'jsonp' instead:
$.ajax({
url: 'https://blockchain.info/address/13DKdDeZcdLbGoNquWz8nHBnVpfBVkDitz?format=json',
dataType: 'jsonp',
success: function (data) {
$('#blockchain').append(JSON.stringify(data));
},
error: function () {}
});
I have backend webService on tomcat server.always when I perform any request (post, get),code enters on error method.
jQuery code is:
var jqxhr = $.ajax(
{
type:"GET",
url:url
success: function() {
alert("success");
},
error: function() {
alert("fail");
}
});
my url is:
http://"192.168.20.220":6060/NostServices/rest/user/resend_activation_email/mailex#blah.com
error message on log is:
XMLHttpRequest cannot load http://"192.168.20.220":8080/NostServices/rest/user/resend_activation_email/dsgg#sdfkmj.com.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://"192.168.20.220":8383' is therefore not allowed access.
web service server is tomcat.
response of this requset can be 1 for success and -1 for mail not valid
and always enter on fail method and when I try to alert response it output [object object]
thanks you for your help
It's a JSONP request so your server side script needs to return data wrapped in callback:
For example
var url = 'http://example.com/req.json?callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json);
},
error: function(e) {
console.log(e.message);
}
});
And on server side:
jsonCallback({});
As your error says your code is failing because of the cross domain error problem. A very detailed answer is found here:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access
Please put in some effort to google these issues, this is a very common thing your trying and all errors have been encountered before and have been resolved somewhere. People will get very annoyed here if you show no effort to debug it yourself or provide all the required detail such as:
All relevant code
Print out of any variables in the code e.g. URL's
Any error message
A list of things you have tried
On a side note a URL should not have quotes it it. It should not be
http://"192.168.1.1":.........
it should be:
http://192.168.1.1:........