【问题标题】:Doing POST with AJAX returns "Response for preflight is invalid (redirect)"使用 AJAX 进行 POST 返回“预检响应无效(重定向)”
【发布时间】:2016-11-26 21:50:54
【问题描述】:

每次我尝试在我的网络服务中执行带有 AJAX 的 POST 时,都会收到“预检响应无效(重定向)”。我已经为我的浏览器下载了一个 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!");
      }
    });
  });
});

【问题讨论】:

    标签: ajax cors


    【解决方案1】:

    如果你想发出这样的复杂请求,服务器需要实际实现 CORS-preflight 协议。标准中有说明:https://fetch.spec.whatwg.org/#http-cors-protocol

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-21
      • 2016-10-20
      • 2017-04-25
      • 2016-06-20
      • 2017-11-15
      • 1970-01-01
      • 2019-03-21
      • 2018-02-13
      相关资源
      最近更新 更多