1

I have a javascript application calling an ajax function that looks like this

$.ajax({ url: apiURL, dataType: 'jsonp', success: function(data) {
    if (data.ok) {
        //do things
}}});

the ajax url im trying to access is through etsyapi everything works fine and dandy until i try to access the application in chrome with adblock on. it makes the ajax call fail completely, returns an error with a Failed to load resource-"theActualURL" message.

I couldn't figure out how to get past this in javascript and was told that i need to do a php call to get this working.

Unfortunately, i dont know the first thing about php- ive tried to understand even the basic structure for it, and i havent been able to find any work arounds in javascript, so i think it has to be done with php.

Is there simplest way to call the ajax function in php with a dynamic url(which is passed to the php page from javascript) and have it pass the array back to javascript to maniuplate?

ive gotten this far with the php-

<?php
    $json = array();

    ????????????????????????

    $jsonstring = json_encode($json);
    echo $jsonstring; 
?>

but dont understand how to access a dynamic url from javascript.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Gazow
  • 1,019
  • 2
  • 11
  • 16
  • Usually, what people mean is to use [`cURL`](http://www.php.net/manual/en/curl.examples-basic.php) or [http streaming](http://www.php.net/manual/en/stream.examples.php) server-to-server to get the same data in an intermediary request. Think "middleware", with your PHP file you're using getting asynchronously the JSON request from Etsy API being the "middle". (Note, `cURL` is brittle, I suggest http streaming.) – Jared Farrish Aug 18 '12 at 00:18

1 Answers1

0

If it's genuinely using jsonp you shouldn't need php. Replace your $.ajax... with:

var newScript = document.createElement('script');
newScript.src = apiURL;
document.head.appendChild(newScript);

Ignore the above, I'm out of date with jQuery. Even so, you shouldn't need php if the API endpoint is really responding with jsonp. It sounds like the url was wrong/bad if you're getting an error. Have you tried simply opening apiURL by putting it in your browser's address bar?

jsonp is a work around for cross domain ajax restrictions that wraps the returned data in a javascript function call. This allows you to load it in a script tag and the function is run with the data as a parameter when the script is executed.

Endophage
  • 21,038
  • 13
  • 59
  • 90
  • Did you see the example code and read the description? Chrome, Adblock, and `jsonp` are already referenced. – Jared Farrish Aug 18 '12 at 00:22
  • @JaredFarrish look at his dataType in the $.ajax. You don't normally load jsonp through ajax, you laod it by writing a script tag to the page with the src set to the url of the endpoint. – Endophage Aug 18 '12 at 00:23
  • @JaredFarrish ah, fair enough, when was that added? I have to admit I haven't been keeping up with all the changes in jQuery. – Endophage Aug 18 '12 at 00:27
  • 1.4, 1.5ish? It's been a while, maybe a year or so. Wait until ver. 2 comes out (not too long, I don't think). See my comment under the question; this is a middleware question. – Jared Farrish Aug 18 '12 at 00:28
  • @JaredFarrish Sounds about right. Been doing predominantly Python for the last year, very little js. – Endophage Aug 18 '12 at 00:30
  • @JaredFarrish However, if the ETSY API is responding correctly with jsonp, he still shouldn't need to use PHP right? That hasn't changed in the last year has it? :-P – Endophage Aug 18 '12 at 00:31
  • No, and I don't know anything about the Etsy API, but... Apparently the advice that's been given is use a proxy (server) to access and return the Etsy API JSON. – Jared Farrish Aug 18 '12 at 00:32