I am working on a projec开发者_运维技巧t that requires me to POST some data to a url that requires a username and password for access. How do I build the URL so that it automatically logs into the system?
$.ajax({
url: "https://xxxxxxx",
type: "POST",
data: "___PUT_BODY___="+file+"&file="+time,
})
If the page uses "Basic" HTTP authentication (the kind where the browser opens a little dialog window asking for a username and password), you can simply do:
https://username:password@www.example.com/
If the site has a login form embedded on the page somewhere, then the login information doesn't go in the URL. You would have to simulate submitting that form (by generating a similar AJAX request specifically for logging in).
Read the funny manual
Also, don't make your query string the way it comes out in the URI bar, make it an object. Also, semi-colon goes at the end of that statement.
$.ajax({
url: "https://example.com",
type: "POST",
data: {
"___PUT_BODY___=" : file,
"&file=" : time
},
password : "theP4s$w0rD!",
username : "Bob's big bad world of HTTP Auth"
});
If the page you send from is public, of if the username/password have to remain private you should reconsider putting your username/password in the page.
You can post to a local page on your server, wich in turn can send the data with for example curl to the receiving server.
Also useing curl you can do any kind of authentication you need to.
精彩评论