Rest call using jQuery

穿精又带淫゛_ 提交于 2019-12-11 19:48:57

问题


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);
      });

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/10442588/rest-call-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!