将cURL API调用转换为Ajax

时间:2016-04-27 08:12:09

标签: javascript ajax api rest curl

"5" is not "5 "

我试过写这样的东西但它不起作用!我一直收到错误curl --basic --user {username}:{password} https://www.blueworkslive.com/api/Auth

400

这也是: {$(document).ready(function(){      var xhr = new XMLHttpRequest();

 $.ajax({
     url: "https://www.blueworkslive.com/api/Auth",
     type: "GET", //This is what you should chage
     dataType: "jsonp",
     username: "admin", // Most SAP web services require credentials
     password: "admin",
     processData: false,
     contentType: "application/json; charset=utf-8",
     success: function () {
         alert("success");
         console.log("success");
     },
     error: function (xhr, ajaxOptions, thrownError) {
         console.log("error");
         console.log(xhr.responseText);
         alert(xhr.status);
         alert(xhr.responseText);
     },
 });

});}  错误:Access-Control-Allow-Origin不允许Origin。我认为错误是由于我从本地服务器运行脚本引起的(因为默认情况下原点设置为null)所以我将它部署在应用程序开发服务器上,以便我有一个公共源。同样的错误发生在新的起源 - 即使原来现在不再是null,但是服务器提供了原点!

1 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
            $(function (){
                $.ajax({
                    url: 'https://www.blueworkslive.com/api/Auth', // url checked ...// error 400 not found url
                    data: { 
                        //data
                        username:username,
                        email:email
                    },
                    dataType: 'json',
                    success: function(data)
                    {
                        //result success
                    }
                });
            });
        });