jQuery Ajax 获取 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12322009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:34:06  来源:igfitidea点击:

jQuery Ajax GET JSON

jqueryajaxruby-on-rails-3

提问by Trt Trt

I have this piece of code:

我有这段代码:

$.ajax({
url: '/piece.json',
type: "GET",
dataType: "json",
success: function (data) {
    alert(data);
}
});

I am on a rail3 app, when I type "/piece.json" I get on my browser what I want. But I cant make it alert using Javascript! Why this happens?

我在一个 rail3 应用程序上,当我输入“/piece.json”时,我在浏览器上得到了我想要的东西。但我无法使用 Javascript 使其保持警惕!为什么会发生这种情况?

Solved, had to use this:

解决了,不得不使用这个:

complete: function (data_response) {

    alert(data_response.responseText);
},

回答by Peter

It's possible that piece.jsondoesn't returns valid JSON so jQuery discards it. Try to change dataType: "json"to dataType: "html"and see if the alert(data)shows the output. If the alert shows your data then there's a syntax error in the json object.

可能piece.json不会返回有效的 JSON,因此 jQuery 会丢弃它。尝试更改dataType: "json"dataType: "html"并查看是否alert(data)显示输出。如果警报显示您的数据,则 json 对象中存在语法错误。

回答by phil

This worked for me:

这对我有用:

$.getJSON( "example.json", function(data) {
   console.log( "success" );
}).done(function() {
   console.log( "second success" );
})
.fail(function() {
  console.log( "error" );
})
.always(function() {
   console.log( "complete" );
});

Replace example.json, with the url or path of your json file. Note the use of $.getJSON vs $get . Here's my reference: http://api.jquery.com/jquery.getjson/

将 example.json 替换为 json 文件的 url 或路径。注意 $.getJSON 与 $get 的使用。这是我的参考:http: //api.jquery.com/jquery.getjson/

回答by Jim Buck

Try changing the datatype to JSONP. It's usually to overcome the Same Origin Policy but may work in your case.

尝试将数据类型更改为 JSONP。通常是为了克服同源政策,但可能适用于您的情况。

$.ajax({
    url: '/piece.json',
    type: "GET",
    dataType: "jsonp",
    success: function (data) {
        alert(data);
    }
});

回答by ck3g

try to use completecallback instead of success. I don't why but successcallback doesn't fires even if all data and response are correct. Looks like some kind of bug

尝试使用complete回调而不是success. 我不知道为什么,但success即使所有数据和响应都正确,回调也不会触发。看起来像某种错误

回答by Phrogz

Presumably you don't have a working route for /piece.json. Use the Developer Tools in your browser and log from the rails app to see what is going on with the XHR request.

大概您没有/piece.json. 使用浏览器中的开发者工具并从 rails 应用程序登录以查看 XHR 请求发生了什么。

(Look at the http response in the browser.)

(查看浏览器中的 http 响应。)

Note that you can more simply use:

请注意,您可以更简单地使用:

$.get('/piece.json',function(data){
  // data is a JS object parsed from a JSON response
},'json');

回答by DadViegas

Add this and see what is returned

添加这个并查看返回的内容

error: function (error) {
    alert(JSON.stringify(error));
}