Search code examples
javascriptxmlhttprequestclientjsonpcors

Using cross origin JSON from third party on client side js?


While this question has been asked before I want to confirm that I'm understanding the problem fully:

I have a javascript browser client app that will load from mydomain.com. I have a third party JSON resource which I would like to use within my app.

The third party resource is on a different domain (http://campbx.com/api/xticker.php) and returns plain JSON. It is on a server that I have no control over.

From my understanding I cannot use JSONP because the resource does not have the proper JSONP callback support (padding).

I also can't use CORS or plain old XMLHttpRequest because the resource has no 'Access-Control-Allow-Origin' header.

So onto the main question... is there a way to use this resource within my client side app without writing a backend proxy? Am I right about my options? Can I somehow use JSONP for a resource that spits back pure JSON?

So far I have a "Uncaught SyntaxError: Unexpected token : " error when I try to load the resource in script tags and 'No Access-Control-Allow-Origin' header error when I attempt to use CORS.


Solution

  • Elaborating on my comment

    No you cannot use this without a proxy. However the proxy will be a few lines of PHP

    <?php 
    header("Content-type:application/json");
    echo file_get_contents('http://campbx.com/api/xticker.php');
    ?>