2

I want to send a username and password in an ajax call. I realize its a not a good practice. But for testing at the time being, I have to do that.

My code so far:

$.ajax({ url: $(this).attr('action'), type: 'POST', data: $(this).serialize(), data: {username: "user", password: "password"}, success: function(data){ //var data = JSON.parse(jsondata); console.log(data); }, }); 

But I run into 401 error everytime. Is there a way I can fix it?

1 Answer 1

5

username and password for http authentication has their own fields

$.ajax({ url: $(this).attr('action'), type: 'POST', data: $(this).serialize(), username: "user", password: "password", success: function(data){ //var data = JSON.parse(jsondata); console.log(data); }, }); 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this and still it throws 401 error. Could it be because the end point might have different name for username and password field?
What type of authentication does the request require?
The end point I am posting to is on https. I looked a little and came across http authentication like username:[email protected]. Should I be doing adding that in my form action like action="username:[email protected]/path/to/the/end/point.html"
Try using a form with that action to see if it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.