10

I'm making cross-domain ajax request to get some data. The REST service have Basic authentication (set through IIS).

$.ajax({
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            dataType: "jsonp",
            contentType: "application/javascript",
            data: myData,
            async: false,
            crossDomain: true,
            url: "http://xx.xx.xx.xx/MyService/MyService.svc/GetData",
            success: function (jsonData) {
                console.log(jsonData);
            },
            error: function (request, textStatus, errorThrown) {
                console.log(request.responseText);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });

When I make this request, it prompts me to enter credentials & I have to manually enter credentials to get the response. Can we send those credentials through the request itself?

0

3 Answers 3

6

Pass username and password like following code,

$.ajax({
    type: "GET",
    xhrFields: {
        withCredentials: true
    },
    dataType: "jsonp",
    contentType: "application/javascript",
    data: myData,
    async: false,
    crossDomain: true,
    url: "http://xx.xx.xx.xx/MyService/MyService.svc/GetData",
    success: function (jsonData) {
        console.log(jsonData);
    },
    error: function (request, textStatus, errorThrown) {
        console.log(request.responseText);
        console.log(textStatus);
        console.log(errorThrown);
    }
    username: username,
    password: password,
});
Sign up to request clarification or add additional context in comments.

Comments

5
$.ajax({  
    url: 'yoururl',
    username : username,
    password :password,
    type: 'POST',
    contentType: 'application/x-www-form-urlencoded',
    dataType: "text",
    xhrFields: 
    {
        withCredentials: true
    },
    beforeSend: function (xhr) { 
        xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));             
    }
});

2 Comments

I'm not excited about this answer, and here's why: it's redundant (question was answered about a half-year earlier) and it's redundant (jQuery handles the auth stuff for you, AFAICT). If I'm wrong about the latter, edit the answer, and I'll delete this comment.
Using the beforeSend property worked for me, since username and password for some reason weren't adding the Authorization header. Just in case it helps anyone.
0
$.ajax({//No need to pass credentials 
            type: "get",
            async: false,
            url: "https://someurl/",
            xhrFields: {
                withCredentials: true
            },            
            success: function (data) {
                //do something
            }
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.