停止XHR回应OPTIONS要求

时间:2019-02-04 22:21:02

标签: javascript ajax xmlhttprequest

我正在构建一个将在Azure函数中托管的API和一个将在Azure Blob存储中托管的Web应用程序。这意味着我的API必须响应来自用户浏览器的OPTIONS请求(使用Postman或Curl时这不是问题)。

我的API对OPTIONS请求进行了适当的响应,因此可以消除所有CORS问题。

当服务器响应客户端Javascript上的OPTIONS时,就会发生此问题。似乎将其作为响应,而忽略了实际响应。如何阻止XHR接受OPTIONS响应?

这是我的功能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PriceEvent
{

    public class PriceChangingEvent : EventArgs
    {

        public int Counter = 0; 

        public int CurrentPrice {

            get
            {
                return CurrentPrice; 

            }

            set
            {
                // only invoke the Event when the Class PriceChangingEvent has been instantiated
                if (Counter > 0) // that is when we are not using the constructor
                {
                    CallPriceChanger(this);
                }

                CurrentPrice = value;
                ++Counter; 

            }

        }
        public int NewPrice { get; set; }

        // 2 args Constructor , constructor invokes setter 
        public PriceChangingEvent(int currentprice, int newprice)
        {
            this.CurrentPrice = currentprice;  // invokes the setter of CurrentPrice
            this.NewPrice = newprice;

        }


         //1. define a delegate between publisher and subscribers
         //source publisher who triggers the event,  
        public delegate void CurrentPriceChangeEventHandler(object source, PriceChangingEvent PriceEvent);

        // 2. define an event
        public event CurrentPriceChangeEventHandler PriceChange;

        // 3. raise the event, OnDataTrained is the method which calls the delegate
        protected virtual void OnCurrentPriceChange(PriceChangingEvent PriceEvent)
        {
           PriceChange.Invoke(this, PriceEvent);
        }

        // 3.Function which raises the event, OnPriceChanger is the method which calls the delegate
        protected virtual void OnPriceChanger(PriceChangingEvent PriceChangingEvent)
        {
            // this: the class 
            PriceChange.Invoke(this, PriceChangingEvent);
        }


        // Function to call the function OnPriceChanger
        public void CallPriceChanger(PriceChangingEvent PriceChangingEvent)
        {

            OnPriceChanger(PriceChangingEvent);

        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            PriceChangingEvent p = new PriceChangingEvent(20, 30);

            p.CurrentPrice = 45;



        }

        //subscribers

        public static void Display(PriceChangingEvent p)
        {
            Console.WriteLine("Current Price has been changed to {0}", p.CurrentPrice);

        }
    }
}

这可行,但是我不能使用它,因为我的应用程序需要能够等待功能的响应,然后再继续进行下一个操作:

function makeRequest(method, url, data) {
  document.body.style.cursor = "wait";
  return new Promise (function (resolve, reject) {
    let xhr = new XMLHttpRequest();
    xhr.open(method, url)
    xhr.onload = function () {
      if (this.status == 200) {
        authToken = this.getResponseHeader('Authorization');
        document.body.style.cursor = "initial";
        console.log('responseText');
        console.log(this.responseText);
        resolve(JSON.parse(this.responseText));
      } else if (this.status == 403) {
        logout();
        document.body.style.cursor = "initial";
        reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
      } else {
        document.body.innerHTML = 'Error: ' + this.responseText;
        document.body.style.cursor = "initial";
        reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
      }
    };
    xhr.onerror = function () {
            document.body.innerHTML = 'Error: ' + this.responseText;
            document.body.style.cursor = "initial";
            reject({
                status: this.status,
                statusText: xhr.statusText
            });
        };
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.setRequestHeader("Authorization", authToken);
        xhr.send(data);
  });
} 

想法非常感谢!

1 个答案:

答案 0 :(得分:-1)

@Quentin是正确的,XHR没有像我最初所想的那样回应OPTIONS的回应。服务器正在发送格式错误的响应