I am trying to obtain the returned information from the following URL using jQuery:
Var URL = http://getdeeplink.linksynergy.com/createcustomlink.shtml?token=2e9830b7ed11c5dee96dd4b4a9e83da216c9d583ae1e617bcafdd6eb004e3c86&mid=13508&murl=http://itunes.apple.com/us/music-video/if-you-think-this-song-is/id413824018?uo=4
I have tried the two approaches outlined below, but in both cases the program just runs past the code blocks like nothing happened. Can any one tell what I'm doing wrong?
$.ajax({
type: "GET",
dataType: "jsonp",
url: URL,
success: function(data){
alert(data);
}
});
Second Approach:
$.get(URL, function(data) {
alert('Load was performed.');
alert(data);
});
First off, if the domain of the server you are requesting the URL is not the same as the domain of your page, then you may be running into same-origin-limitations.
Second, If it's not the same origin and you're trying to use JSONP (which is looks like you might be), then your server will need to specifically support JSONP and package the result in JSONP format which means formatting the result as an executable script and also means respecting the callback=xxxx
query argument on the URL.
Third, this piece of javascript you put in your question looks like it's missing quotes around the URL to make it legal javascript and var
is all lowercase:
var URL = "http://getdeeplink.linksynergy.com/createcustomlink.shtml?token=2e9830b7ed11c5dee96dd4b4a9e83da216c9d583ae1e617bcafdd6eb004e3c86&mid=13508&murl=http://itunes.apple.com/us/music-video/if-you-think-this-song-is/id413824018?uo=4";
For further debugging, I would suggest that you attach an error handler for the ajax call and see if that provides any further information. If something is being blocked due to same-origin issues or you just have other script errors that are stopping execution of your script, then your browser error console or debug console may also offer you a helpful message.
You cannot perform an ajax call like that because of same origin limitation. What you need to do is setup a server side page which your java script will perform an ajax call on say 'getdeeplink.php' (I'm assuming you are doing it in PHP)
Your javascript
$.get('getdeeplink.php',data,function(){.....});
Its in getdeeplink.php where you perform the REST call and echo back the response to the ajax handler
In getdeeplink.php
$rest_response = file_get_contents('http://getdeeplink.linksynergy.com/createcustomlink.shtml?token=2e9830b7ed11c5dee96dd4b4a9e83da216c9d583ae1e617bcafdd6eb004e3c86&mid=13508&murl=http://itunes.apple.com/us/music-video/if-you-think-this-song-is/id413824018?uo=4');
echo json_encode($rest_response); //send the result to the ajax handler