最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why i am always getting 401 (Unauthorized) error in case jquery ajax - Stack Overflow

matteradmin7PV0评论

I am using ajax in jquery. when i submit my value, I am getting this error every time. Please let me know where i am wrong?

  <script>
    $(document).ready(function(){
      $("button").click(function(){
        $.ajax({
          url: 'url_path',
          data: { 
            user : {
              email : $('#email').val(), 
              password : $('#password').val()
            } ,
            vendor_identifier : '3231-43423-fdsdfs'
          },
          dataType: 'jsonp',
            //method: 'POST',
            type:'POST',
            success: function(msg) {
              var msg = $.parseJSON(msg);
              console.log(msg);
            }
          });
      });
    });
  </script>

I am using ajax in jquery. when i submit my value, I am getting this error every time. Please let me know where i am wrong?

  <script>
    $(document).ready(function(){
      $("button").click(function(){
        $.ajax({
          url: 'url_path',
          data: { 
            user : {
              email : $('#email').val(), 
              password : $('#password').val()
            } ,
            vendor_identifier : '3231-43423-fdsdfs'
          },
          dataType: 'jsonp',
            //method: 'POST',
            type:'POST',
            success: function(msg) {
              var msg = $.parseJSON(msg);
              console.log(msg);
            }
          });
      });
    });
  </script>
Share Improve this question edited Mar 7, 2018 at 7:12 CommunityBot 11 silver badge asked Mar 5, 2014 at 11:13 user3319135user3319135 912 gold badges3 silver badges11 bronze badges 10
  • What is url: 'url_path'(It's a string) ? – Ishan Jain Commented Mar 5, 2014 at 11:15
  • What is 'url_path' this is not the correct URL – Pavlo Commented Mar 5, 2014 at 11:15
  • 2 The code you've shared will give us some idea of what you are sending to the server, but since we can't see the server side code - we have no idea what it is doing with the data to determine that you are not authorised. – Quentin Commented Mar 5, 2014 at 11:15
  • 2 dataType: 'jsonp' and type:'POST' are inpatible though. a JSON-P request is handled by generating a script element with a src attribute. That will always be a GET request. – Quentin Commented Mar 5, 2014 at 11:16
  • I have put my local path here , where i want to send this data and this data reaching perfectly. But its response which i am getting back is 401 error. – user3319135 Commented Mar 5, 2014 at 11:17
 |  Show 5 more ments

1 Answer 1

Reset to default 1

From your example it seems that you are accessing some API which requires authentication through the email and password. The 401 Unauthorized is simply saying that the remote server was for some reason unable to authenticate the user with the bination of email and password and possibly the vendor_identifier that you sent with the request.

Chances are that the API expects the username (email) and password in the form of Basic Authentication in the Authorization HTTP header. If you are using JQuery >= 1.7.2 then it will create the Authorization header for you.

see this question: How to use Basic Auth with jQuery and AJAX?

$(document).ready(function(){
  $("button").click(function(){
    $.ajax({
    url: 'url_path',
      data: { 
      user : {
        email : $('#email').val(), 
        password : $('#password').val()
      },
      vendor_identifier : '3231-43423-fdsdfs'
    },
    username: $('#email').val(),
    password: $('#password').val(),
    dataType: 'jsonp',
    success: function(msg) {
    var msg = $.parseJSON(msg);
    console.log(msg);
  }
});
Post a comment

comment list (0)

  1. No comments so far