How to get the JSON from external server - javascript

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.

Related

JSONP Parse Error with valid JSON Output

I am calling a Cross Domain AJAX Request using JSONP. Now, i am able to make it work using CORS. But, i want to know why it is not happening with JSONP. I looked at other threads with similar problem but couldnt figure out why it is not working in my case.
Here is the code:
$.ajax({
type: "GET",
url: http://XXXX:8000/sap/bc/test_rest/jsonp_test?mode=S&ticket=123,
dataType: "jsonp",
jsonp: false,
jsonpCallback: "myJsonMethod",
success: function (data) {
alert(data);
},
error: function (httpReq, status, exception) {
alert(status + " " + exception);
}
});
Now, this calls my server, the data is populated and then i get an alert "parse error myJSONMethod was not called" on a callback URL http://xxxx:8000/sap/bc/test_rest/jsonp_test?mode=S&ticket=123&_=1470322282936
Additionally, in the console i get the error as Uncaught SyntaxError: Unexpected token :
The response structure is:
{"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}}
When i run this JSON Output on jsonlint it says it is a valid JSON. I don't know where the JSON is getting messed up.
Your data must be like this to be valid for JSONP:
myJsonMethod({"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}});
the responsetext must be valid js code and it will run immediately when the response is over.So if you write your code
{"ROOT":{"CONTRACT":"40002356","ITEM":"000010","KUNNR":"0000004676","NAME":"REALTY
EXECUTIVES","NET_PRICE":19.95,"GROSS_PRICE":19.95,"MATNR":"144","SQFEET":""}}
in a <script> tag,the console will throw an err like that

Uncaught SyntaxError: Unexpected token < in <!DOCTYPE html>

i am sending a ajax request to external domain. Here is my code,
There might be an issue on JSONP response while converting the html data to jsonp. I have tried so many solution because i am requesting to cross domain so i have to use JSONP else i have to face cross domain error. Error when Use simple JSON ERROR: " XMLHttpRequest cannot load http://www.blink.com.kw/search-result.aspx?text=apple&searchfor=all. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:49324' is therefore not allowed access."
Response error:
Uncaught SyntaxError: Unexpected token <
<script type="text/javascript">
$(document).ready(function(){
$("#bt").click(function(){
$.ajax({
type: 'GET',
url: 'http://www.blink.com.kw/search-result.aspx?text=apple&searchfor=all',
dataType: 'jsonp',
success: function (data) {
console.log(data);
//$("#data").html(data);
}
});
});
});
</script>
This is probably happening because you are specifying it as JSONP, which executes the data as a script in order to execute a callback function. If it sends back a normal HTML document with the doctype being the first line it sees, this would occur.
Try this code, basically we should not use url like this.
Also, this url is not return any json or jsonp format, please check your link as well
<script type="text/javascript">
$(document).ready(function(){
$("#bt").click(function(){
$.ajax({
type: 'GET',
url: 'http://www.blink.com.kw/search-result.aspx',
dataType: 'jsonp',
data:{
text: apple,
searchfor: all
}
success: function (data) {
console.log(data);
}
});
});
});
</script>
Hope this helps :)

jquery ajax always throws error (never becomes success)

Below is my piece of code.
My issue is the function on success never gets called.
It always throws the error section.
$.ajax({
url: 'http://localhost/zd/get_list',
data: 'term=' + medical_provider,
dataType: 'json',
type: 'GET',
success: function(data){
alert('success');
},
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR);
}
});
when i alert jqXHR is get [object Object] and when I alert textStatus I get "error" and when I alert errorThrown I get a blank error message.
I am using jquery 1.11.0.
I never get the success alert.
Your help is greatly appreciated.
Also the the data returned in json is properly validated. I did a test on jsonlint.
Your get_list server-side functionality is erroring.
Use chrome developer tools to breakpoint and inspect your jqXHR object in the error callback, it will have more details about what is going wrong.
Edit:- based on your updated comments, try adding:
contentType: 'application/json',
to your ajax call parameters
So it turns out, the base url (was using codeigniter) was my computers ip address instead of localhost. Accessing the through the ip addy OR changing the base url to localhost worked.
Thank you all for the help.

Uncaught SyntaxError: Unexpected token : ajax call

So I'm trying to query the below json feed, however I keep getting the error in the topic.
I've searched around this site for possible answers however none that I've come across have worked so far. Commented out datatype and jsonp, jsonpCallback isnt it either, either is data, I've made sure that it validats via http://jsonformatter.curiousconcept.com/ and it does. I really dont know.
$.ajax({
type: 'GET',
url: 'http://raidbots.com/json/playerdata/us/mannoroth/usiris',
cache:true,
dataType: 'jsonp',
data: {
format: 'json',
},
success: ranks,
jsonpCallback:'callbackName',
error: function(data) { console.log(data); },
jsonp: false,
});
function callbackName(data){
console.log("jsonpCallback");
}
var ranks = function(data) {
console.log(data);
}
Thank you
-Art
The error is in your JSONp data because it's just JSON and not JSONp. JSONp requires the document to be valid JavaScript containing a function call.
If they don't support jsonp you need to use a proxy script (e.g. a php script on your server that retrieves the document) or ask them to send CORS headers so you can use a normal non-JSONp AJAX call to retrieve the data directly.

how to get error message of failed ajax

I want to consume a webService which I get JSON Data from
$.ajax({
type: 'GET',
contentType: 'application/json',
dataType: 'JSON-P',
//the url shows the correct JSON Data when I copy the URL in my browser
url: 'http://url.com',
success: function(data){
console.log(data);
alert(data);
},
error: function(xhr, testStatus, error){console.log("Error occured: "+error+" "+xhr+" "+testStatus)}
})
In Firefox, It calls back with the error function, but I dont know what the problem is, it would be great to know the error message but my approach doesnt work.
thanks in advance
edit: In Chrome I get the following error:
Origin null is not allowed by Access-Control-Allow-Origin.
edit: It´s solved, the problem was indeed that json doesnt work cross site, and the data was not "jsonp-conform" (it had to set a function(json data...))arround the json data. This could be done by changing the url at the end "_&jsonp=successCallback. Sorry for bothering you
I think there was a JSON object syntax error. Check your JSON object syntax error with jsonlint.com.
Are you getting this error locally?
Try it on a server and if it doesn't work: XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

Categories

Resources