[英]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.