使用 AJAX 执行 POST 会返回"Response for preflight is invalid (redirect)"



我每次尝试在我的Web服务中执行ajax的帖子时,都会得到" preflight无效(重定向)"。我已经为浏览器下载了CORS插件,并且可以使用它执行" get"请求。

$(function(){
var $name = $("#nameTxtB");
var $order = $("#orderTxtB");
var $price = $("#priceTxtB");
var $link = "http://localhost:51834/CoffeeService.svc/addorder";
$("#addButton").on('click', function(){
var $try1 = $price.val();
var $parse = parseInt($try1);
console.log($parse);
var CoffeeOrders = {
Name: $name.val(),
Order: $order.val(),
Price: $parse,
  };
console.log(CoffeeOrders);
$.ajax({
 contentType : "application/json; charset=utf-8",
 dataType : 'json',
 type: 'POST',
 url: $link,
 data: CoffeeOrders,
 success: function(){
 alert("Order was sucessfully created!");
 },
  error: function(){
  alert("Something went wrong!");
      }
    });
  });
});

如果要提出这样的复杂请求,服务器需要实际实现CORS-PRFLIGHT协议。它在标准中进行了解释:https://fetch.spec.whatwg.org/#http-cors-protocol。

相关内容

最新更新