JavaScript, JSONP and reading XML from cross-domain

前端 未结 4 1506
离开以前
离开以前 2020-12-15 01:58

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data comes from domain B)

I have a solution that uses JSONP but I really ne

4条回答
  •  死守一世寂寞
    2020-12-15 02:52

    The usual solution is to have a "AJAX proxy" - a simple server-side script running on your domain, that fetches the data from the other domain and returns it unchanged.

    The simplist is to give the script the URL you need the data from:

    http://example.com/proxy.php?url=http%3A%2F%2Fexample.org%2Fajax%3Fid%3D123 gets the data from http://example.org/ajax?id=123

    This can however be misused if you let any URL be fetched like that, so you should have your script, check that it actually only gets data from a specific URL.

    In order to avoid having to parse the URL to check this, you could write a proxy specificly for your app, that only accesses the specific resource you need:

    http://example.com/proxy.php?id=123 to access http://example.org/ajax?id=123.

提交回复
热议问题