使用AJAX执行POST会返回“预检的响应无效(重定向)”

时间:2016-11-25 15:17:40

标签: ajax cors

每当我尝试使用AJAX执行POST到我的webservice时,我得到“预检的响应无效(重定向)”。我已经为我的浏览器下载了一个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!");
      }
    });
  });
});

1 个答案:

答案 0 :(得分:1)

如果你想制作类似的复杂请求,服务器需要实际实现CORS预检协议。它在标准中解释:https://fetch.spec.whatwg.org/#http-cors-protocol