Hi guys I am doing project in html, jquery (localhost) to retrieve data from Server it is already running in localhost.
When I run below url in browser (default method is GET for browser) url_bar, I got the output perfectly in JSON format.
http://localhost:8080/xxx/yyy/st.json?callback=callback&_=1419402270660
But, when I call the above url in html file (calling implicitly the appropriate .js file)
ajax like below code :
var url_var = "http://localhost:8080/xxx/yyy/st.json";
$.ajax({
url : url_var,
method : "GET",
headers : {
'Accept': 'application/json',
'Content-Type' : 'application/json'
},
dataType: 'jsonp',
jsonpCallback: 'callback',
success : function(data){
console.log(data);
},
failure : function() {
alert("failure");
},
error : function(){
}
});
I got below error in console of browser
Uncaught SyntaxError: Unexpected token : st.json?callback=callback&_=1419400635006:1
Reason for "jsonp" format used in ajax is that to return the servlet response without the conflict of "Cross browser domain issue" .
Any way to resolve this. Alternate way also welcome. Thanks in advance guys.....
Related
I am trying to fetch data from text file which resides on server. I have access of that location and able to see content when I put URL in browser tab.
I am trying to make AJAX call and get file content, but I am getting Error: Uncaught SyntaxError: Unexpected identifier
Code
function logResults(json){
console.log(json);
}
$.ajax({
url: u,
dataType: "jsonp",
jsonpCallback: "logResults"
});
on console,
I tried below code too, but same result,
$.ajax({
type: 'GET',
url: u,
crossDomain: true,
dataType: 'jsonp',
async: false,
headers: {
"Access-Control-Allow-Origin": "*"
},
success: function(succ) {
console.log("Success: ", succ)
},
error: function(err) {
console.log("Error: ", err)
}
});
This code is always going into error block.
You said:
dataType: "jsonp",
But the URL is responding with:
Deployment automatically finished…
Which is not JSONP, it is plain text.
You get the error when it tries to execute the plain text as JSONP.
Don't put wrong information in the dataType field.
async: false,
That's deprecated. Don't use it. It's also pointless since you are using callbacks.
It's also incompatible with JSONP so it is ignored (until you remove the incorrect dataType).
crossDomain: true,
This has no effect unless you are making a:
non-JSONP request
to the same origin
which gets redirected to a different origin
… which is very rare.
headers: {
"Access-Control-Allow-Origin": "*"
},
Access-Control-Allow-Origin is a response header. It doesn't belong the request. Trying to add it to the request will cause extra problems as it will make the request preflighted.
(At least, that would be the case if you hadn't said dataType: 'jsonp' because adding request headers is incompatible with JSONP).
All you need on the client
The only code you need on the client is:
function logResults(result){
console.log(result);
}
$.ajax({
url: u,
success: logResults
});
The server you are requesting the data from will need to use CORS to grant you permission to access it if it is a cross-origin request.
It is because you have added dataType as jsonp, so that it will try to parse the response to JSON and if the response is not a JSON it will throw error.
I'm trying to get images from Instagram public api via ajax and JSONP:
var target = https://www.instagram.com/p/BP3Wu_EDXsjdT5Llz13jFv2UeS0Vw0OTxrztmo0/?__a=1?callback=?';
$$.ajax({
type: "GET",
dataType: 'json',
crossDomain: true,
url: target,
success: function(data){
console.log(data);
},
error: function(xhr,status){
console.log("Error"+status);
}
});
I'm getting: Uncaught SyntaxError: Unexpected token <.
What's wrong?
Thanks
A few mistakes...
var target = 'https://www.instagram.com/p/BP3Wu_EDXsjdT5Llz13jFv2UeS0Vw0OTxrztmo0/?__a=1&callback=';
Changes: Missing ' at the beginning and changed second ? with &
Should work fine
That API with ?__a=1 is undocumented API and does not support JSONP, so you cannot make client side API call using AJAX, you have to make a server side http request and it will work.
thanks in advance!
I am trying to do a Jquery.ajax() to get the JSON from this link:
API.
This is the code I use:
$.ajax({
url : "https://api.9292.nl/0.1/locations/station-amsterdam-centraal/departure-times?lang=nl-NL",
dataType: "json",
success:function(data)
{
console.log('gelukt');
},
errror: function (data) {
console.log(data);
}
});
But it gives the error "No 'Access-Control-Allow-Origin' header is present", so i tried to set dataType to 'jsonp', but then a error shows up with: 'Uncaught SyntaxError: Unexpected token :'
Anyone got an idea, how to get the JSON from that API?
You would help me a lot!
Thanks
Read about JSONP here or here, most probably you need to pass a callback parameter into query.
Running on Chrome I get this error message:
Uncaught SyntaxError: Unexpected token
This is the part of my code which is responsible for the request:
function wetter() {
$.ajax({
'Accept': 'application/json',
type: 'GET',
url: '[here comes the url',
dataType: 'jsonp',
success: function (data) {
//content
}
});
};
You are trying to make a jsonp request to a script that sends json, similar to this. You cant just drop a p after the json and expect it to work.
The datatype it's receiving is probably not what it expects. You may be returning a JSON object while the expected result is JSON with padding (JSONP). You can either try returning JSONP type data or change the datatype in your code above to JSON.
I've got the following problem: I need to download a JSON file from an API via JQuery / JavaScript. In theory this should be quite basic.
I tried $.ajax and all of its siblings like $.get or $.getJSON. I alway get an 200 OK but my Firebug reports an error. Printing the error just says: "error" - so not that helful.
I read that maybe the JSON file is corrupt. So I tried it with a plain text file (*.txt). Same result.
The JSON file is valid, I check it against a validator.
I also tried ContentType and dateType and experimented with json and jsonp...
I basically used something like this (with a million variations for testing purposes):
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
...
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.statusText);
}
});
Am I missing something important here? It's really odd that nothing seems to change the behavior of the AJAX-call.
In fact I don't really need AJAX because I need to grab the JSON file when loading the page...
And the JSON file is not on the same domain as the AJAX caller.
Is that URL located on the same server you're trying to get the data from?
If not, you ran into a cross-domain request, which can only be handled using JSONP. So, the JSON file itself must be compatible with JSONP format, otherwise jQuery won't be able to process it (even if you provide a 'jsonp' dataType).
P.S.: Firebug will always show response code 200 but give an empty response body for such requests
Try in this way by disabling security
$.ajax( {
type : 'GET',
contentType : "application/json; charset=utf-8",
url : surl, \\specify your url
async : false,
dataType : 'json',
headers : {
Accept : "application/json",
"Access-Control-Allow-Origin" : "*"
},
crossDomain : true,
success : SucceedFunc,
error : function(data, textStatus, errorThrown) {
console.log("error" + ' ' + JSON.stringify(data) + ' ' + textStatus + ' ' + errorThrown);
}
});
function SucceedFunc(data) {
alert("success");
}
}
Did you try to catch the error the correct way?
$.ajax({
url: 'http://www.myurl.com/api/v1/myfile.json',
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error.message);
}
});
If you are using chrome go to cmd prompt and run the chrome by disabling the security. You can disable security using pathwhere_chrome_is_located\chrome.exe --disable-web-security
and run the html page. I think this may help you.