$.ajax vs $.post:获取允许源错误

$.ajax vs $.post : getting allow origin error

本文关键字:许源 错误 获取 ajax vs post      更新时间:2023-09-26

使用这个做ajax很好:

$.post("http://localhost:3000/scrape",
            {
                data: json
            },
            function(data, status){
            });

但这不是吗?

$.ajax({
              type: 'POST',
              url: 'http://localhost:3000/scrape',
              data: json,
              contentType: "application/json",
              success: function(data,status){
              },
              async:false
            });

使用 $.ajax 我得到了错误XMLHttpRequest 无法加载 http://localhost:3000/scrape。对预检请求的响应未通过访问控制检查:请求的资源上不存在"访问控制允许源"标头。

我以为两个都一样?这里有什么问题?

在第一个示例中,您正在使用application/x-www-form-urlencoded数据发出一个简单的请求。

在第二个示例中,您指定的内容类型不是简单请求允许的内容类型之一,因此您将触发预检请求。

服务器愉快地响应了 POST 请求,但对印前检查选项请求没有响应。