gpt4 book ai didi

jquery - 如何修复 'Missing required request header. Must specify one of: origin,x-requested-with' Ajax 错误

转载 作者:行者123 更新时间:2023-12-04 00:22:53 35 4
gpt4 key购买 nike

我正在尝试使用 CORS 将 ajax 调用从我的本地机器运行到外部 URL,其中任何地方的 url 都附加到实际的 url/端点。但是,它会导致“缺少必需的请求 header ”。必须指定以下之一:origin,x-requested-with' 错误。我已经手动设置了我的 header ,如下面的代码所示,我只是不太明白为什么在明确定义“requested-with”值后仍然会发生这种情况。

        // Set URL to the Create patient endpoint
const Url = 'https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/mmsgatewayapistaging/api/patient/create';

// Define User data here ** Currently static information **
// Need to set variables based on input value to the correct identifier
// EX: "FirstName": $('#first_name').val();
const userData = {
"RequestingClient": {
"ClientId": "XXXXXXXXXX",
"ClientSecret": "XXXXXXXXXX",
"MemberId": "XXXXXXXXXX"
},
"Pharmacy": {
"IdentifierType": 2,
"Identifier": "5164086800"
},
"LastName": "Test",
"MiddleInitials": "X",
"FirstName": "Seth",
"Gender": "M",
"DOB": "01/01/1990",
"Email": "seth@test.com",
"PhoneNumber": "1234567890",
"MobileNumber": "1234567890",
"BusinessNumber": "1234567890",
"PatientIdentifiers": [
{ "IdentifierType": 1, "IdentifierType": "12345" }
],
"AddressInformation": {
"AddressType": 1,
"StreetLine1": "123 Test",
"StreetLine2": "",
"City": "Waco",
"State": "TX",
"ZipCode": "76710",
},
"ExternalPatientId": "1234567890",
"Timestamp": "2019-12-09T17:59:15.7624947Z"
};

// On button ajax call to create a new user with the above data
$('.btn').click(function () {
$.ajax({
url: Url,
type: "POST",
dataType: "json",
contentType: "application/json",
// set the request header authorization to the bearer token that is generated
headers: {
"X-Requested-With": "XMLHttpRequest",
"Authorization": "Bearer " + responseToken,
},
data: userData,
success: function (result) {
console.table(result);
$('.output_userInfo').html(result.ErrorMessage);
},
error: function (error) {
console.log(`Error ${error}`)
},
});


});

最佳答案

您正在正确设置标题,但是根据 cors-anywhere 的作者的说法,由于以下原因,您可能会遇到与发出请求相关的错误(即使在设置了适当的 header 之后):

  • 您要代理的 URL 无法访问 (例如,该站点已关闭,或者他们阻止了对 CORS Anywhere IP 的访问)。
  • 在给定的时间范围内,给定的来源向 CORS Anywhere 发送了太多请求 ( #45 )。
  • URL 本身被列入黑名单 (例如 #32#42 )。
  • CORS Anywhere 已关闭。 (即这适用于自托管)

  • 基于向您的目标 URL ( https://staging.micromerchantsystems.com/ ) 发出请求,我收到了一个 IIS 启动画面,因此您可能想要验证一切是否都在您的终端上运行。使用下面的一个非常小的示例,我似乎能够访问正确的站点,但是收到了 401 错误,表明我未经授权(但我没有收到 400 header 要求的消息):
    $(function() {
    $.ajax({
    url: "https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/mmsgatewayapistaging/api/patient/create",
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    // set the request header authorization to the bearer token that is generated
    headers: {
    "X-Requested-With": "XMLHttpRequest"
    },
    success: function(result) {
    console.log(result);

    },
    error: function(error) {
    console.log(`Error ${error}`)
    },
    });
    });

    我想如果您包含适当的授权信息,您应该能够访问它。如果您仍然遇到问题,您可能需要 consider reaching out to the author谁可以帮助进一步解决问题。

    关于jquery - 如何修复 'Missing required request header. Must specify one of: origin,x-requested-with' Ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59272380/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com