简体   繁体   中英

How to access SSL enabled WCF service from jQuery?

I want to access a SSL enabled WCF service from asp.net 2.0 application. Site was hosted on IIS 7.

Here is the Code

function getCourse() {
    $.ajax({
        type: "GET",
        url: "https://website.com/service.svc/task",
        processData: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 7000,
        username: "someusername",
        password: "somepassword",
        success: function (data) { alert("success"); },
        error: function (xhr, status, error) { alert("error"); }
    });
};

I get 401 error, I didn't do any configuration in iis or webconfig related to the ssl access? Am I missing anything here? or the fact that I'm using asp.net 2.0 application cause the error?

Well I think you might need to add additional header in your jquery ajax function, like following one:

headers: { 
    Authenticate: Basic username:password (to base64)
}

or

beforeSend: setHeaders

function setHeader(xhr) {
    xhr.setRequestHeader('Authorization', 'Basic base64.encode(username:password)');
}

According to Wiki, 401 Unauthorized is defined as

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.

The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.

Reference HTTP status codes: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Basic access authentication: http://en.wikipedia.org/wiki/Basic_access_authentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM