Javascript 如何在javascrript中使用AJAX从URL获取JSON数据
当我试图在浏览器控制台中打印响应时。它显示了这个错误Javascript 如何在javascrript中使用AJAX从URL获取JSON数据,javascript,json,Javascript,Json,当我试图在浏览器控制台中打印响应时。它显示了这个错误 "Cache-Control": "no-cache", 跨源读取阻止(CORB)使用MIME类型text/html阻止跨源响应。有关更多详细信息,请参阅` "Cache-Control": "no-cache", "Cache-Control": "no-cache", "Cache-Control": "no-cache", 这是我的代码: var setting
"Cache-Control": "no-cache",
跨源读取阻止(CORB)使用MIME类型text/html阻止跨源响应。有关更多详细信息,请参阅`
"Cache-Control": "no-cache",
"Cache-Control": "no-cache",
"Cache-Control": "no-cache",
这是我的代码:
var settings = {
async: true,
crossDomain: true,
crossOrigin: true,
url: "http://35.230.52.177:8095/OneSoftTracking/rest/tracking/consignment/AE101632?hostname=aastha-enterprises.com",
method: "GET",
"headers": {
"Content-Type":"application/json",
"Access-Control-Allow-Origin":"*",
'Access-Control-Allow-Methods':'GET',
'Access-Control-Allow-Credentials' : true,
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "bea2b074-eefc-473e-84d7-b680a07ed7df,dafa4f5c-94af-4efe-967f-75a9fe185a1e",
},
success: function(data) {
console.log("+++++SuCCESS");
console.log(data);
},
error: function(error){
console.log("NOT SUCCEED");
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
"Cache-Control": "no-cache",
你还没有给我们看你真正的代码
"Cache-Control": "no-cache",
错误消息在查询字符串中清楚地显示了一个回调
,它不在URL中,不应该添加,因为您在jQuery ajax配置中没有说数据类型:“jsonp”
"Cache-Control": "no-cache",
真正的代码配置为使用JSONP
"Cache-Control": "no-cache",
JSONP响应是一个JavaScript程序(特别是用一组参数调用单个函数的程序)。您从服务器得到的响应是一个HTML文档(内容类型是text/HTML
,而不是application/javascript
)
"Cache-Control": "no-cache",
CORB错误是浏览器告诉您响应是HTML而不是JSONP,因此它拒绝尝试执行它
"Cache-Control": "no-cache",
Postman屏幕截图显示您希望得到JSON(仍然不是JSONP)响应(但您也没有得到)
"Cache-Control": "no-cache",
您需要:
"Cache-Control": "no-cache",
- 将服务器更改为使用JSONP响应
- 更改请求以请求正确的数据(也可能更改服务器以授予您使用CORS读取数据的权限)
进一步阅读:
"Cache-Control": "no-cache",
关于代码的注释:
async: true,
"Cache-Control": "no-cache",
这是默认设置,请删除它
crossDomain: true,
"Content-Type": "application/json",
"Cache-Control": "no-cache",
这是跨源请求的默认设置,请删除它
crossOrigin: true,
method: "GET",
"Access-Control-Allow-Origin": "*",
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Credentials': true,
"Cache-Control": "no-cache",
这是跨源请求的默认设置,请删除它
crossOrigin: true,
method: "GET",
"Access-Control-Allow-Origin": "*",
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Credentials': true,
"Cache-Control": "no-cache",
这是默认设置,请删除它
crossDomain: true,
"Content-Type": "application/json",
"Cache-Control": "no-cache",
您正在发出GET请求。没有描述请求类型的请求正文。这是胡说八道。摆脱它
crossOrigin: true,
method: "GET",
"Access-Control-Allow-Origin": "*",
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Credentials': true,
"Cache-Control": "no-cache",
这些是响应头,而不是请求头。把它们放在请求上是胡说八道。摆脱他们
"Accept": "*/*",
"Cache-Control": "no-cache",
违约几乎肯定会好起来。您可能不需要覆盖它
"Cache-Control": "no-cache",
主配置选项“cache”可能比显式设置头更好
"Cache-Control": "no-cache",
"Postman-Token": "bea2b074-eefc-473e-84d7-b680a07ed7df,dafa4f5c-94af-4efe-967f-75a9fe185a1e",
您的服务器关心这一点的可能性微乎其微。您可能可以摆脱它。基于响应中的CORS头,浏览器会阻止此类请求。@AnkurR-否。这是CORB而不是CORS!主席先生,在我的编码中,我必须改变什么:主席先生,请建议我在这方面必须做什么改变,以打印json数据console@Sandeep_Sh-更改服务器以使用JSONP响应,或更改请求以请求正确的数据(也可能更改服务器以授予您使用CORS读取数据的权限)@Sandeep_Sh-更改服务器以使用JSONP响应,或更改请求以请求正确的数据(也可能更改服务器以授予您使用CORS读取数据的权限)
"Cache-Control": "no-cache",