Cross-domain ajax request basic authentication

前端 未结 2 1870
失恋的感觉
失恋的感觉 2020-12-06 06:25

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

$.ajax({
             


        
相关标签:
2条回答
  • 2020-12-06 07:09

    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,
    });
    
    0 讨论(0)
  • 2020-12-06 07:19
    $.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));             
        }
    });
    
    0 讨论(0)
提交回复
热议问题