Sinatra sinatrarb 返回的 json ajax 拿不到响应

ptmagic · September 12, 2012 · Last by nouse replied at September 12, 2012 · 3207 hits

大家好,我最近做了一个小的实验, 用 sinatra 做 API 服务器代码如下 get '/example.json' do content_type :json { :key1 => 'value1', :key2 => 'value2' }.to_json end

然后通过网页输入http://0.0.0.0:4567/example.json 能看到 json

然而我用 jquery 和 ajax 去请求 API 服务器的时候缺发现响应没有任何东西 jquery 代码如下: $.get('http://0.0.0.0:4567/example.json', function(data){ alert(data) }); $.getJSON("http://0.0.0.0:4567/example.json", function(json){ alert("JSON Data: " + json.users[3].name); }); 没有响应我任何的 json,麻烦各位大大帮忙

自己找了一下解决方案,貌似要在 ajax 请求里面加入一个 callback 方法 完成的链接应该是 $.getJSON("http://0.0.0.0:4567/example.json?callback=?") 各位大大有什么要补充的吗?

使用$.get 最好指定用 json 解析

You need to Sign in before reply, if you don't have an account, please Sign up first.