JavaScript for HTTP post XML with authentication
JavaScript for HTTP post XML with authentication
我想实现类似的东西curl --digest -u 用户名:密码 -d "mydata" http://example.com/push使用 Javascript 作为 chrome 插件。这是我的代码。
<html>
<head>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type='text/javascript' src="jquery.base64.js"></script>
<script language="javascript">
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', $.base64Encode('username:password'));
}
$.ajax({
type: "POST",
url: "http://example.com/push",
data: "<Phone><Data>mydata</Data></Phone>",
xhrFields: {
withCredentials: true
},
crossDomain: true,
beforeSend: setHeader,
success: function (data,status,xhr){
//do something
}
});
</script>
</body>
</html>
这是一个具有 HTTP 基本身份验证的跨域 POST。由于 CORS,chrome 浏览器而不是 POST 会发送 OPTIONS 消息质询。如果我将网址更改为同一域,它可以工作。如果我禁用了身份验证,它也可以工作。但是我必须POST到另一个域,这不受我的控制,这意味着我无法使用easyXDM之类的解决方案。它们还需要 HTTP 基本身份验证。我一直在寻找解决方案一个星期。任何提示都值得赞赏。
你试过吗:
xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) //usage: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
参考: http://www.w3.org/Protocols/HTTP/1.0/spec.html#BasicAA
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- jQuery AJAX Cross Domain with BASIC Authentication
- github basic authentication with javascript (Appcelerator)
- JavaScript for HTTP post XML with authentication
- Authentication with JWT
- Authentication and authorization with Flatiron's Resourc
- Drop down user Authentication with Angular