簡體   English   中英

jQuery中的CORS GET請求

[英]CORS GET request in jQuery

我正在嘗試發出一個核心請求,該請求應返回一堆json數據,但是由於某種原因該請求失敗。 到目前為止,我完成的代碼如下:

$(document).ready(function () {
var url = 'http://test/v/calls';
$.ajax({
    url: url,
    beforeSend: function (request) {
        request.setRequestHeader("Authorization", "Bearer test");
    },
    success: function (json) {
        console.log("Success", json);
    },
    error: function (textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    }
});

響應如下:

catcher.js:197 OPTIONS http://test/v/calls 401 (Unauthorized)

和:

XMLHttpRequest cannot load http://test/v/calls. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://some_domain' is therefore not allowed access. The response had HTTP status code 401.

注意:具有授權參數的相同url在Postman中可以正常使用。 看不出什么毛病。

您需要在服務器上啟用cors。 由於Postman的協議處理跨站點腳本的方式,因此它在Postman中工作。 您的服務器基於什么? 以及如何將cors添加到端點? 您可以從兩個問題開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM