[英]Jquery ajax call returns error when the rest service is returning a string
尝试从我的休息服务发送一个简单的文本,并使用ajax调用读取它。 找到了很多关于jsonp和跨浏览器兼容性的答案,也试过跨域。
这是休息服务:修剪所有内容只发送一个简单的字符串。
@GET
@Path("/getcontents2")
@Produces({MediaType.TEXT_PLAIN})
public String getContents2(@QueryParam("name") String msg) {
return "abc";
}
ajax调用:
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://metrics/getcontents2?name=Work/loc.txt',
crossDomain: true,
async: false,
dataType:'html',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
console.log(xhr.responseText);
console.log(xhr);
},
});
});
浏览器按原样打开字符串。 我想在jquery脚本中出现了一些问题。
Firebug出错:
GET http://metrics/getcontents2?name=Work/loc.txt 200 OK 4ms
0
(an empty string)
(an empty string)
Object { readyState=0, status=0, statusText="error"}
修复!
这是因为我的服务器不支持跨域。 配置它将corsfilter,它就像一个魅力!
尝试将您的数据类型设置为
文本
jsonp
dataType: 'jsonp',
http://api.jquery.com/jQuery.ajax/
您必须执行以下两项操作之一才能进一步解决错误;
return "{"text":"abc"}"; //or something like this
json中编码的字符串return "{"text":"abc"}"; //or something like this
return "{"text":"abc"}"; //or something like this
jquery跨域请求仅允许用于dataTypes“script”和“jsonp”。
我已经更新了你的小提琴它仍然会抛出一个错误,但这与解析json错误有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.