使用ajax将curl请求发送到API

时间:2019-05-11 20:40:57

标签: jquery ajax

我想使用ajax进行API的发送和获取,但是每次都会收到CORS错误。

我尝试将此行代码添加到ajax,但仍然无法正常工作

xhrFields: { withCredentials:true },

并收到此错误

Access to XMLHttpRequest at 'http://mywebsite.com/api/users/request_login' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我的ajax函数

$(document).ready(function(e) {

    $("form[ajax=true]").submit(function(e) {

        e.preventDefault();

        $("#loadingimg").show();

        $.ajax({
            url: "http://mywebsite.com/api/users/request_login", 
            xhrFields: { withCredentials:true },
            type: 'POST',      
            contentType: 'application/json',
        data: '{"phone_number":"12345678"}',    
            success: function(returnhtml){                          
                $("#result").html(returnhtml); 
                $("#loadingimg").hide();                    
            }           
        });    

    });

});

0 个答案:

没有答案