Sinatra, JavaScript Cross-Domain Requests JSON

后端 未结 4 1492
眼角桃花
眼角桃花 2021-02-06 19:58

I run a REST-API build on top of Sinatra. Now I want to write a jQuery Script that fetches data from the API.

Sinatra is told to response with JSON

befor         


        
4条回答
  •  旧巷少年郎
    2021-02-06 20:05

    Try to call

    $.getJSON("http://example.com/?callback=?",function(data) { alert(data); });
    

    In this sample main keyword is construction "callback=?", so you need to process this param in your server-side script, and make a valid JSONP, like this:

    function({ "foo" : "bar" });
    

    Where "function" is random data, which is generated by jQuery automatically. Read more here about jQuery and cross-domain JSONP.

提交回复
热议问题