Trt*_*Trt 7 ajax jquery ruby-on-rails-3
我有这段代码:
$.ajax({
url: '/piece.json',
type: "GET",
dataType: "json",
success: function (data) {
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
我在rails3应用程序上,当我输入"/piece.json"时,我会在浏览器上找到我想要的内容.但我不能使用Javascript使其警惕!为什么会这样?
解决了,不得不用这个:
complete: function (data_response) {
alert(data_response.responseText);
},
Run Code Online (Sandbox Code Playgroud)
这对我有用:
$.getJSON( "example.json", function(data) {
console.log( "success" );
}).done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});
Run Code Online (Sandbox Code Playgroud)
将 example.json 替换为 json 文件的 url 或路径。注意 $.getJSON 与 $get 的使用。这是我的参考:http : //api.jquery.com/jquery.getjson/
有可能piece.json
没有返回有效的JSON,因此jQuery放弃了它。尝试更改dataType: "json"
为dataType: "html"
,看看是否alert(data)
显示输出。如果警报显示您的数据,则json对象中存在语法错误。
想必您没有 的工作路线/piece.json
。使用浏览器中的开发人员工具并从 Rails 应用程序登录以查看 XHR 请求发生了什么。
(查看浏览器中的 http 响应。)
请注意,您可以更简单地使用:
$.get('/piece.json',function(data){
// data is a JS object parsed from a JSON response
},'json');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
60617 次 |
最近记录: |