140it with AJAX
I try to shorten some text using the http://140it.com/ service over AJAX, but I always get an empty response.
Exa开发者_如何学Cmple:
$.ajax({
url: "http://140it.com/api/shrink",
data: {text: 'hello'},
success: function(d) { alert(d) },
dataType: 'html',
type: 'GET'
});
If I execute the url manually (like browsign the web) I get the response. Fix? Thx.
You cannot perform cross-domain AJAX requests.
I.e. If your site is hosted on domain A.com, you cannot do a $.ajax request to B.com.
One of the solutions is to use a server-side proxy script on your own domain. For instance, you could have PHP do the request to 140it and have jQuery call your script instead of 140it directly. This is described in this article: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html
The other solution is to use JSONP web services which allow you to specify a callback function and can be called by appending a SCRIPT element to your page. However, not all API providers offer JSONP.
- Christian
You should be using the JSONP format of the api which uses the callback parameter
http://140it.com/api/shrink?char_max=5&text=today&callback=do_something
Use jQuery's getJSON and you should be good to go.
精彩评论