我正在尝试使用Ajax发出请求,但收到错误响应
{"readyState":0,"status":0,"statusText":"error"}
。这是我的Ajax调用的代码:
$(document).ready(function() {
$.ajax({
url : "http://localhost:8080/persons/all",
/* url : "https://reqres.in/api/users?page=2", */
type : "GET",
dataType : "json",
contentType : "application/json",
ajaxSuccess : function(data) {
alert("Success " + JSON.stringify(data));
},
beforeSend : function(data) {
alert("BeforeSend " + JSON.stringify(data));
},
complete : function(data) {
alert("Complete " + JSON.stringify(data));
},
success : function(data) {
alert("Success " + JSON.stringify(data));
},
error : function(data) {
alert("Error " + JSON.stringify(data));
}
});
});
这是在浏览器上调用http://localhost:8080/persons/all的结果
{
"2": {
"id":"2",
"firstName":"Steve",
"lastName":"Smith",
"age":21
},
"1": {
"id":"1",
"firstName":"Steve",
"lastName":"Smith",
"age":21
}
}
通过https://reqres.in/api/users?page=2可以对此链接进行Ajax调用 结果是
{
"readyState": 4,
"responseText":"{
\"page\":2,
\"per_page\":3,
\"total\":12,
\"total_pages\":4,
\"data\":[
{
\"id\":4,
\"first_name\":\"Eve\",
\"last_name\":\"Holt\",
\"avatar\":\"128.jpg\"
},
{
\"id\":5,
\"first_name\":\"Charles\",
\"last_name\":\"Morris\",
\"avatar\":\"128.jpg\"
},
{
\"id\":6,
\"first_name\":\"Tracey\",
\"last_name\":\"Ramos\",
\"avatar\":\"o/128.jpg\"
}
]
}",
"responseJSON": {
"page":2,
"per_page":3,
"total":12,
"total_pages":4,
"data":[
{
"id":4,
"first_name":"Eve",
"last_name":"Holt",
"avatar":"128.jpg"
},
{
"id":5,
"first_name":"Charles",
"last_name":"Morris",
"avatar":"128.jpg"
},
{
"id":6,
"first_name":"Tracey",
"last_name":"Ramos",
"avatar":"128.jpg"
}
]
},
"status":200,
"statusText":"OK"
}
为什么对我的本地URL的调用不起作用?我的Web服务位于8080端口,而Web应用程序位于8081。过去10天,我一直在为此苦苦挣扎。请帮忙!!!
答案 0 :(得分:0)
错误 您不能将请求从一个域发送到另一个域!或从本地到其他域的请求!!!
您可以向http标头CORS访问其他doamin的其他域发送请求!