[英]Jquery ajax call returns error when the rest service is returning a string
Trying to send a simple text from my rest service and read it using ajax call. 尝试从我的休息服务发送一个简单的文本,并使用ajax调用读取它。 Found many answers about jsonp and cross browser compatibility, tried crossdomain too.
找到了很多关于jsonp和跨浏览器兼容性的答案,也试过跨域。
Here is the rest service: Trimmed everything down to send only a simple string. 这是休息服务:修剪所有内容只发送一个简单的字符串。
@GET
@Path("/getcontents2")
@Produces({MediaType.TEXT_PLAIN})
public String getContents2(@QueryParam("name") String msg) {
return "abc";
}
The ajax call: 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);
},
});
});
The browsers opens up the string as is. 浏览器按原样打开字符串。 I guess something is really wrong in the jquery script.
我想在jquery脚本中出现了一些问题。
Error on Firebug: 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"}
Fixed it! 修复!
It was because my server was not supporting cross-domain. 这是因为我的服务器不支持跨域。 Configured it will corsfilter and it worked like a charm!
配置它将corsfilter,它就像一个魅力!
try setting your datatype to
text
jsonp 尝试将您的数据类型设置为
文本
jsonp
dataType: 'jsonp',
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
You will have to do one of two things to go with this to resolve the error further; 您必须执行以下两项操作之一才能进一步解决错误;
return "{"text":"abc"}"; //or something like this
return "{"text":"abc"}"; //or something like this
json中编码的字符串return "{"text":"abc"}"; //or something like this
return "{"text":"abc"}"; //or something like this
jquery cross-domain requests are only allowed for dataTypes "script" and "jsonp". jquery跨域请求仅允许用于dataTypes“script”和“jsonp”。
I have updated your fiddle it still throws an error but that is related to a parse json error 我已经更新了你的小提琴它仍然会抛出一个错误,但这与解析json错误有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.