Dashboard Cross-domain AJAX with jquery

前端 未结 5 1920
暖寄归人
暖寄归人 2020-12-19 10:21

Hey everyone, I\'m working on a widget for Apple\'s Dashboard and I\'ve run into a problem while trying to get data from my server using jquery\'s ajax function. Here\'s my

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 10:51

    Cross-domain Ajax requests ( Using the XMLHttpRequest / ActiveX object ) are not allowed in the current standard, as per the W3C spec:

    This specification does not include the following features which are being considered for a future version of this specification:

    • Cross-site XMLHttpRequest;

    However there's 1 technique of doing ajax requests cross-domain, JSONP, by including a script tag on the page, and with a little server configuration.

    jQuery supports this, but instead of responding on your server with this

    {"message":"Hello World","version":"1.0"}
    

    you'll want to respond with this:

    myCallback({"message":"Hello World","version":"1.0"});
    

    myCallback must be the value in the "callback" parameter you passed in the $.getJSON() function. So if I was using PHP, this would work:

    echo $_GET["callback"].'({"message":"Hello World","version":"1.0"});';
    

提交回复
热议问题