2

I want something like this: How to use Basic Auth with jQuery and AJAX?

In the question there they explained how to use authentication

I need to use a jQuery $.getJSON instead of a $.ajax request to a server because it doesn't support cross-site. And also I need to submit a user and a password. Is there a way to submit a user and password as parameters in a $.getJSON WITHOUT sending it in the URL like https://user:password@myserver.com?

Community
  • 1
  • 1
Dennis Konoppa
  • 129
  • 2
  • 12
  • `$.ajax` doesn't support CORS when you use JSONP, it uses JSONP instead of CORS. If you want to make a cross-origin request then the server needs to give you permission with CORS or hack around the same origin policy with JSONP. If you use JSONP you can't set arbitrary HTTP headers. – Quentin Jan 14 '16 at 11:57
  • 1
    Possible duplicate of [Pass request headers in a jQuery AJAX GET call](http://stackoverflow.com/questions/3258645/pass-request-headers-in-a-jquery-ajax-get-call) – Gavriel Jan 14 '16 at 13:01

2 Answers2

-1

$.getJSON is a shorthand for $.ajax, so indeed you probably need ajax. You can see on http://api.jquery.com/jquery.ajax/ how to set headers

$.ajax(url, {crossDomain:true, headers:{'X-My':'Foo'}})

And this is possible duplicate of Pass request headers in a jQuery AJAX GET call

Community
  • 1
  • 1
Gavriel
  • 18,088
  • 12
  • 63
  • 98
  • It won't support a cross-origin request so I need to use $.getJSON. With $.Ajax I would know how to use it – Dennis Konoppa Jan 14 '16 at 14:18
  • as I told you already getJSON calls ajax, see http://stackoverflow.com/questions/5922175/jquery-getjson-vs-ajax-json – Gavriel Jan 14 '16 at 14:20
  • @DennisKonoppa — See my comment on the original question. You appear to be confusing the `getJSON` function with JSONP and the idea of cross-origin requests with CORS. – Quentin Jan 14 '16 at 15:49
  • Setting `crossDomain:true` is usually pointless. It only matters if you are making an XMLHttpRequest (i.e. not JSONP) request to the **same** origin and then performing an *HTTP redirect* to a different origin. – Quentin Jan 14 '16 at 15:50
-1
$.getJSON("url", { user: "name", password: "123456" })
    .done(function(json) {
        console.log("Store User autthentication successfully");
    })
    .fail(function(jqxhr, textStatus, error) {
    });
MarredCheese
  • 9,495
  • 5
  • 59
  • 63
neha gupta
  • 30
  • 3