JSON cross site without JSONP

后端 未结 5 925
半阙折子戏
半阙折子戏 2021-01-22 15:15

I have a local piece of hardware that I access via it\'s IP address to retrieve json data.

I am creating a web based app but it can not access the local json data when t

相关标签:
5条回答
  • 2021-01-22 15:50

    As Brad mentioned using a proxy is the way to go. Here's some more information on proxies:

    http://developer.yahoo.com/javascript/howto-proxy.html

    If you have access to a php server, I can recommend the proxy by http://www.troywolf.com/articles/. You'll need the following:

    • http://www.troywolf.com/articles/php/class_http/proxy.phps
    • http://www.troywolf.com/articles/php/class_http
    0 讨论(0)
  • 2021-01-22 15:53

    If your url looks something like this http://<IPAddress>/RequestedJson then you might use flXHR which is quite convenient.

    1) Import the libraries of flXHR
    2) Simply create an instance of flXHR
    3) Then assign it to jQuery Ajax options

    $.ajaxSetup({
       "xhr": myflXHRInstance
    });
    

    (more info in ajaxOptions page)
    4) Put crossdomain.xml in the root of your ip i.e. http://<IPAddress>/crossdomain.xml More info about crossdomain.xml

    5) Enjoy your crossdomain requests via jQuery ))

    P.S. Requires the flash plugin to be installed. There are almost no browsers that do not have Flash player plugin installed. Riastats

    EDIT: Sorry for russian link for crossdomain.xml.
    Crossdomain.xml example

    0 讨论(0)
  • 2021-01-22 16:04

    You can create a local proxy.

    For example, if your web app is running at www.example.com, make a small server-side component that responds at www.example.com/hardware.

    Then, upon calling this URL the server-side component performs a direct query to the hardware and returns it to the calling webapp.

    Things to note:

    • If the response is large or the hardware is slow, your proxy may have to buffer a lot. If you want to make a solution that scales, you'll have to be careful.
    • You can do caching of the data at your proxy to reduce the load on the hardware
    • You can massage the data before sending it to the web app in your proxy. For example, change from JSON to XML or vice versa. Or even pre-format it to HTML...
    0 讨论(0)
  • 2021-01-22 16:05

    You can proxy it with a PHP/ASP/etc. page on your local site that (in the back-end) will query the remote information. This will allow you to call somesite.com/getForeignData (which actually calls othersite.com/foreignData) and return it within the same domain.

    Other than that, you're going to need to use JSONP (this is a boundary purposely set). The "last" option is to enable cross-boundary calls on your browser, but if you want others to have the same ability, this is only a temporary solution.

    0 讨论(0)
  • 2021-01-22 16:05

    Can you add header to the HTTP Response? If yes, add the following

    Access-Control-Allow-Origin with the value *

    then your browser will allow the request for that resource

    0 讨论(0)
提交回复
热议问题