简体   繁体   中英

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.

You can use only a server side proxy .

Your understanding that you cannot use JSONP or CORS is right.

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');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM