4

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>
Community
  • 1
  • 1
user3319135
  • 91
  • 1
  • 3
  • 11
  • What is url: 'url_path'(It's a string) ? – Ishan Jain Mar 05 '14 at 11:15
  • What is `'url_path'` this is not the correct URL – Pavlo Mar 05 '14 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 Mar 05 '14 at 11:15
  • 2
    `dataType: 'jsonp'` and `type:'POST'` are incompatible though. a JSON-P request is handled by generating a script element with a src attribute. That will *always* be a GET request. – Quentin Mar 05 '14 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 Mar 05 '14 at 11:17
  • You should also look at the Net tab in your browser's developer tools to make sure that the request being submitted is the request you think is being submitted. I have a nasty suspicion that you are expecting the browser to POST some JSON (which it isn't). – Quentin Mar 05 '14 at 11:17
  • Then what i should use instead of JSON @Quentin – user3319135 Mar 05 '14 at 11:24
  • You are currently using `application/x-www-form-urlencoded`, which is fine, so long as the (still mysterious) server side code expects it. – Quentin Mar 05 '14 at 11:25
  • if i am not using jsonp then i am getting this error------ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access – user3319135 Mar 05 '14 at 11:35
  • There's no shortage of questions about dealing with the same origin policy. JSONP is one approach if you are willing to live with making the API completely public. The server side code (which you **still** haven't shared with us) has to be able to parse the request (which can't be POST or JSON) and send a JSONP response. – Quentin Mar 05 '14 at 11:36

1 Answers1

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 combination 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);
  }
});
Community
  • 1
  • 1
Peter Hudec
  • 2,272
  • 3
  • 18
  • 25