I'm trying to get currencies rate from the URL:
CURRENCY
It works perfect from browser and also from Android java using REST API.
Now I'm trying to get it from JS using JQUERY AJAX.
I use this code:
function handleJson(data)
{
console.log(data);
}
$.ajax({
url: 'http://www.boi.org.il/currency.xml?curr=01?jsoncallback=handleJson',
contentType: 'text/xml',
success: function(result) {
alert("XML File is loaded!");
alert(result);
},
async: true,
dataType:"jsonp",
type:"get"
});
I see in the log that content arrives but I can't handle it.
Here the screenshot
Related
I have a strange issue using jQuery and JSON, especially JSONP.
My goal is to simply GET JSON data, but I always end up with the following error:
Uncaught SyntaxError: Unexpected token
Here is the code:
<script type="text/javascript">
$(document).ready(function() {
var myurl = "someurl";
$.ajax({
url: myurl,
method: 'GET',
contentType: 'application/javascript',
dataType : 'jsonp',
success: function(result){
//Do something with JSON result
}
});
</script>
And of course the JSON (raw format):
{"result":[{"targetView":"powerUsage","myData":{"someItems":["9","5","8"],"someItems2":[{"text":"protoText","currentRecord":"45.38","absolute":100}]}}]}
I tried the webservice with the Advanced Rest Client App in Google Chrome and it is working perfectly. I have no clue why this simple example gets this syntax error message.
Your Ajax code looks like fine. I think you are trying to make a Cross domain call as JSONP is a hack for dealing cross domain ajax call. If you Server code if ready for dealing with JSONP request then you must have send a callback parameter like
?callback=my_callback_method
than you service will return result with a callback see below links for more details:
https://learn.jquery.com/ajax/working-with-jsonp/
http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery
You missed to put ready function close, that is }); at the last before script tag close:
<script type="text/javascript">
$(document).ready(function()
{
var myurl = "someurl";
$.ajax(
{
url: myurl,
method: 'GET',
contentType: 'application/javascript',
dataType: 'jsonp',
success: function(result)
{
//Do something with JSON result
}
});
});
</script>
I am trying to use JQuery to access Transloc's API (https://www.mashape.com/transloc/openapi-1-2#!documentation). I have not used JQuery or this API before. If anyone could shed some light on this I would appreciate it.
I can't test this as I don't have a key but it should work in theory.
Using the endpoint you linked to. Change the url for what you need.
$.ajax({
type:"GET",
beforeSend: function (request)
{
request.setRequestHeader("X-Mashape-Authorization", <mashape-key>);
},
url: "https://transloc-api-1-2.p.mashape.com/agencies.json?agencies=12%2C16&geo_area=35.80176%2C-78.64347%7C35.78061%2C-78.68218&callback=call",
processData: false,
success: function(data) {
//use data
},
error: function(data) {
//handle error
}
});
I am trying to call my service from here
http://diningphilospher.azurewebsites.net/api/dining
using below javascript,
$.ajax(
{
type: "GET",
dataType: "json",
url: "http://diningphilospher.azurewebsites.net/api/dining/",
success: function (data)
{
alert(data);
}
});
But I am getting error relating cross origin. I see people suggest using JSONP but I guess my server does not support JSONP. I studied CORS and could not understand the head or tail of it. I would like to know the way around to read the JSON that sits in different domain.
I hope this should work:
$.ajax(
{
type: "GET",
dataType: "jsonp",
url: "http://diningphilospher.azurewebsites.net/api/dining/",
success: function (data)
{
alert(data);
}
});
Or just add/append ?callback=? along with your cross-domain url.
I have links of messages coming in, say in an email message body. The issue right now is I want to click on the content and be able to display the unwrapped link in the right message pane (I use jQuery).
I am using jQuery and figured that using AJAX to call another PHP script to send the requests to bit.ly, etc. would work. I am not 100% sure how to do this.
The main issue is that some of these links are 2-3 times shortened as Twitter has their own automatic shortner. Any help on this would be appreciated.
Thanks.
You could use a service called LongURL. The API call in jQuery would look something like this:
$.ajax({
url: 'http://api.longurl.org/v2/expand?format=json&url=http%3A%2F%2Fbit.ly%2Fv20RLs',
dataType: 'jsonp',
method: 'get',
success: function(response) {
alert(response['long-url']);
}
});
or
$.ajax({
url: 'http://api.longurl.org/v2/expand',
data: {
format: 'json',
url: 'http://bit.ly/v20RLs'
},
dataType: 'jsonp',
method: 'get',
success: function(response) {
alert(response['long-url']);
}
});
Our server is located in europe.
Now and then an american based user reports a problem when he uses the $.getJSON function.
His browser just displays the json response instead of catching it and passing it to javascript.
The ajax call just looks like:
$.getJSON(url, function(json_data){ ... });
Any ideas?
More info:
The the same user has the problem in FF and IE.
I use Ruby On Rails render :json. which response type is application/json.
Try using the $.ajax() method so you can handle errors and debug the success callback.
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
url: url,
success: function(json_data) {
// parse json_data object
},
error: function (xhr, status, error) {
// check for errors
}
});
Aside from that using an XHR viewer like Firebug or Chrome's built-in utility (CTRL+SHIFT+I) can be very helpful.
XHR DOM reference: http://www.w3schools.com/dom/dom_http.asp