admin管理员组

文章数量:1026989

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);
  }
});

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);
  }
});

本文标签: javascriptWhy i am always getting 401 (Unauthorized) error in case jquery ajaxStack Overflow