jQuery ajax GET 返回 405 方法不允许
我正在尝试访问需要身份验证的 API。这是我正在使用的代码,但我不断收到 405 Method Not allowed 错误。有什么想法吗? (我的用户名和密码是正确的)
function basic_auth(user, pass){
var tok = user + ':' + pass;
var hash = $.base64.encode(tok);
return "Basic " + hash;
}
var auth = basic_auth('username','password');
var releaseName1 = "11.6.3";
var releaseName2 = "11.6.3 Confirmed";
$.ajax({
type: "GET",
url: "https://www10.v1host.com/Company/rest-1.v1/Data/Story?sel=Description,Number,Name,Timebox.Name,Parent,AssetState&where=Custom_Release2.Name='"+releaseName1+"','"+releaseName2+"';AssetState='64'",
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', auth);
},
dataType: "xml",
async: false,
success: parseXml
});
function parseXml(xml){
$(xml).find("item");
}
I am trying to access an API that requires authentication. This is the code that i am using but i keep getting a 405 Method Not Allowed error. Any thoughts? (my username and password are correct)
function basic_auth(user, pass){
var tok = user + ':' + pass;
var hash = $.base64.encode(tok);
return "Basic " + hash;
}
var auth = basic_auth('username','password');
var releaseName1 = "11.6.3";
var releaseName2 = "11.6.3 Confirmed";
$.ajax({
type: "GET",
url: "https://www10.v1host.com/Company/rest-1.v1/Data/Story?sel=Description,Number,Name,Timebox.Name,Parent,AssetState&where=Custom_Release2.Name='"+releaseName1+"','"+releaseName2+"';AssetState='64'",
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', auth);
},
dataType: "xml",
async: false,
success: parseXml
});
function parseXml(xml){
$(xml).find("item");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法跨域进行 javascript/Ajax 调用(没有一些严重的拼凑)。我们过去为此所做的是创建一个本地 jsp 代理,我们使用 javascript 调用该代理,但它只是到远程 URL 的传递。
下面是一些示例 JSP 代码,它与我用来访问返回 JSON 的 SOLR 实例的代码很接近。
You can't make javascript/Ajax calls across domains (without some serious kludging). What we've done for this in the past is to create a local jsp proxy that we call with our javascript but is just a pass-through to the remote URL.
Here is some example JSP code that is close to what I've used to hit a SOLR instance returning JSON.
如果您正在尝试发送跨域请求,请尝试将您的数据类型更改为“jsonp”
try to change your datatype to "jsonp" is you are trying to send a cross domain request