[support] jQuery.ajax returns 404
Seth Freach
sfreach at gmail.com
Fri May 21 21:29:33 UTC 2010
Scott,
Using firebug in Firefox will let you see exactly what goes back and
forth in an ajax request.
However, below will not work because, as a security feature, ajax
requests can only be sent to the same domain that the page originated
from. Ie, if your site is example.com, then ajax GETs and PUTs can only
be performed back to example.com, not to other sites directly from the
client browser such as api2.citysearch.com.
What you need to do is make a server side wrapper. Your ajax call
should be back to your own site, example.com, which can then take the
request and with PHP build a curl request to api2.citysearch.com. Once
your server has received the response from citysearch, then it can
deliver it as the reply to the original ajax request.
Seth
Scott wrote:
> I'm still experimenting with a new citysearch module. I've managed to
> fetch back the json with curl but I'd still like to let ajax handle it.
> I have this function but it reports a 404 error. Is there a way to
> determine the url value actually being sent? The generated value I'm
> using here works both with window open() and with my curl function.
> My code:
>
> function processSearch(form) {
> var what = js_trim(form.what.value);
> var where = js_trim(form.where.value);
> var loc = js_trim(form.location_or_event.value);
> var publisher = js_trim(form.publisher.value);
> var api_key = js_trim(form.api_key.value);
>
> var URL = 'api2.citysearch.com/search/'+loc+'?what='+what
> +'&where='+where+'&format=json&publisher='+publisher
> +'&api_key='+api_key;
>
> alert(URL);
> $.ajax({
> type: 'get',
> url: URL,
> dataType: 'jsonp',
> success: function() { alert("Some success"); },
> error:function (xhr, ajaxOptions, thrownError){
> alert(xhr.status);
> alert(xhr.statusText);
> alert(xhr.responseText);
> }
> }); // end ajax method
> }
>
> function js_trim(str) {
> return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
> }
>
>
>
More information about the support
mailing list