Jquery 如何修复';缺少必需的请求标头。必须指定以下其中一项:原点、x-request-with';Ajax错误

Jquery 如何修复';缺少必需的请求标头。必须指定以下其中一项:原点、x-request-with';Ajax错误,jquery,ajax,Jquery,Ajax,我正在尝试使用实际URL/端点前面的CORS anywhere URL从本地计算机运行一个ajax调用,以访问外部URL。但是,这会导致“缺少必需的请求头”。必须指定以下选项之一:原点、x请求和“错误”。我已经手动设置了我的头,正如您在下面的代码中所看到的,我只是不太明白为什么在显式定义'requested with'值之后仍然会发生这种情况 // Set URL to the Create patient endpoint const Url = 'https:

我正在尝试使用实际URL/端点前面的CORS anywhere URL从本地计算机运行一个ajax调用,以访问外部URL。但是,这会导致“缺少必需的请求头”。必须指定以下选项之一:原点、x请求和“错误”。我已经手动设置了我的头,正如您在下面的代码中所看到的,我只是不太明白为什么在显式定义'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
的作者,您可能会遇到与发出请求相关的错误(即使在设置了适当的标题之后),原因如下:

  • 无法访问您要代理的URL(例如,站点已关闭,或者他们已阻止访问CORS Anywhere IP)
  • 在给定的时间范围内,给定来源向任何位置的CORS发送的请求过多
  • URL本身被列入黑名单(例如,)
  • CORS Anywhere已关闭。(即,如果您是自托管,则此选项适用)
基于对目标URL的请求(
https://staging.micromerchantsystems.com/
),我将获得一个IIS启动屏幕,因此您可能需要验证您的终端是否正在运行所有内容。使用下面一个非常简单的例子,我似乎能够访问正确的站点,但是收到了一个401错误,表明我未经授权(但我没有收到400头所需的消息):


我想,如果您包括适当的授权信息,您应该能够访问它。如果您仍然遇到问题,您可能想知道谁可能会帮助您进一步排除问题。

我对能够成功运行的邮递员代码做了进一步的挖掘,并从中得到了正确的答案。下面是我用来正确运行API和跨域传递信息的代码

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

        // On button ajax call to create a new user with the above data

        $('.btn').click(function () {
            // The input value variables NEED to be defined and set after the click function
            // so that the value can be received and passed into the userData variable.

            // Define User data here ** Currently static information **
            // Need to set variables based on input value to the correct identifier
            // EX: "FirstName": $('#first_name').val();
            var user_firstName = $("#first_name").val();

            const userData = {
                "RequestingClient": {
                    "ClientId": "XXXXXX",
                    "MemberId": "XXXXXXX"
                },
                "Pharmacy": {
                    "IdentifierType": 2,
                    "Identifier": "XXXXXXX"
                },
                "LastName": "Test",
                "MiddleInitials": "X",
                "FirstName": user_firstName,
                "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"
            };

            // Using stringify is an important part in successfully passing the data
            var userString = JSON.stringify(userData);



            var userSettings = {
                "async": true,
                "crossDomain": true,
                "url": Url,
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "Authorization": "Bearer " + responseToken,
                    "Accept": "*/*",
                },
                "data": userString
            }

            $.ajax(userSettings).done(function (response) {
                console.table(response);
            });
        });

谢谢你提供的信息!我刚刚备份到您所处的位置,得到了相同的401错误。我被引导相信这是我在头中实现的承载令牌。请仔细检查一下,我在上面的示例中设置它的方式是否正确?我不完全确定,但我认为它是正确的(假设支持承载令牌)。关于如何处理专门针对
cors anywhere
的身份验证场景,没有大量文档。您是否尝试过一些更简单的(如果可能的话),比如使用基本身份验证(例如用户名:密码)用于<代码>认证> /代码>页眉?您可能想考虑在特定的回购请求上发布一个问题,因为作者可能更熟悉这种情况。
        // Set URL to the Create patient endpoint        
        const Url = "https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/mmsgatewayapistaging/api/patient/create";

        // On button ajax call to create a new user with the above data

        $('.btn').click(function () {
            // The input value variables NEED to be defined and set after the click function
            // so that the value can be received and passed into the userData variable.

            // Define User data here ** Currently static information **
            // Need to set variables based on input value to the correct identifier
            // EX: "FirstName": $('#first_name').val();
            var user_firstName = $("#first_name").val();

            const userData = {
                "RequestingClient": {
                    "ClientId": "XXXXXX",
                    "MemberId": "XXXXXXX"
                },
                "Pharmacy": {
                    "IdentifierType": 2,
                    "Identifier": "XXXXXXX"
                },
                "LastName": "Test",
                "MiddleInitials": "X",
                "FirstName": user_firstName,
                "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"
            };

            // Using stringify is an important part in successfully passing the data
            var userString = JSON.stringify(userData);



            var userSettings = {
                "async": true,
                "crossDomain": true,
                "url": Url,
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "Authorization": "Bearer " + responseToken,
                    "Accept": "*/*",
                },
                "data": userString
            }

            $.ajax(userSettings).done(function (response) {
                console.table(response);
            });
        });