将cURL API调用转换为Ajax
问题描述:
curl --basic --user {username}:{password} https://www.blueworkslive.com/api/Auth
我试图写这样的东西,但它不会工作!我不断收到错误400
将cURL API调用转换为Ajax
$.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);
},
});
而这也: {$(文件)。就绪(函数(){VAR XHR =新的XMLHttpRequest();
xhr.open("GET", "https://www.blueworkslive.com/api/Auth?version=20091212", false);
xhr.setRequestHeader("Authorization", "Basic " + btoa("username.com:password"));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Access-Control-Allow-Origin: *");
xhr.send("");
alert(xhr.status);
alert(xhr.responseText);
});} 错误:Access-Control-Allow-Origin不允许来源。我认为错误是因我引起的从本地服务器上运行脚本(因为原点设置为默认为空),所以我部署它的应用开发服务器上,以便我有一个公共origin.The的发生同样的错误新的起源 - 即使现在的起源不再为空,但服务器提供了起源!
答
$(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
}
});
});
});
+0
请求的资源上没有“Access-Control-Allow-Origin”头。因此不允许原产地'null'访问。该响应具有HTTP状态代码400。 – Radu
@ blackpla9ue您是否在编辑时添加了评论? – devnull69
@ devnull69 nope。只有缩进。检查编辑历史。我尝试期间 – blackpla9ue
有趣......要编辑我提交了“您的”编辑显示添加注释,现在我可以从历史中看到,这种变化是 – devnull69