简体   繁体   English

发送带有自定义标头的纯Ajax HTTP请求

[英]Sending plain ajax HTTP request with custom header

I have an existing java client on top of which IOS, andriod developers prepared a simple http request based applications. 我有一个现有的Java客户端,其上是IOS,andriod开发人员准备了一个基于http请求的简单应用程序。 And am trying to achieve same in HTML5 app. 并试图在HTML5应用中实现相同目标。

And the difficulty right now am facing is sending an custom header within the AJAX request like authorization with encrypted login details. 现在面临的困难是在AJAX请求中发送自定义标头,例如具有加密登录详细信息的授权。

I tried to achieve same on various REST clients and able to send "AUTHORIZATION : BASIC XXXXXX=" in request header. 我试图在各种REST客户端上实现相同的目标,并且能够在请求标头中发送“ AUTHORIZATION:BASIC XXXXXX =“。 And getting proper json response" 并获得正确的json响应”

在此处输入图片说明

But if i try same using ajax call am not able to send similar request header. 但是,如果我尝试使用ajax调用,则无法发送类似的请求标头。 Request sending as OPTIONS instead of GET and the authorization tag is not going properly as a header instead it's going as "Access-Control-Request-Headers:authorization". 请求以OPTIONS而不是GET的形式发送,并且授权标签不能作为标头正常运行,而是作为“ Access-Control-Request-Headers:authorization”发送。

and here is the snippets i have tried. 这是我尝试过的片段

 <script>
    //$.ajaxSetup({ headers: { 'Authorization': 'Basic XXXXXXX='} })


    // get form data for POSTING       
    var vFD = new FormData(document.getElementById('upload_form'));
    var oXHR = new XMLHttpRequest();
    oXHR.open('POST', "https://123.123.123.123:229/");
    //oXHR.send(vFD);



    var body = 'Basic XXXXXXX=';
    var mUrl = "https://123.123.123.123:229/?json";
    var client = new XMLHttpRequest();
    client.open('GET', mUrl, true);
    client.withCredentials = true;
    client.crossDomain = true,

    client.setRequestHeader('Authorization', 'Basic XXXXXXX=');
    client.send(body);



    simpleHttpRequest();
    function simpleHttpRequest() {
        alert("calling ");

        var headers = {
            "Authorization": "Basic XXXXXXX="
        };
        $.ajaxSetup({ "headers": headers });
        $.ajaxSetup({ 'cache': false });
        $.ajax({
            type: "GET",
            withCredentials: true,
            //                data: {
            //                    address: 'http://www.google.com'
            //                },
            crossDomain: true,
            Headers: { "Authorization": "Basic XXXXXXX=" },
            dataType: "jsonp",
            url: mUrl,
            cache: false
        });
    }

    xhrToSend();
    function xhrToSend() {
        // Attempt to creat the XHR2 object
        var xhr;
        try {
            xhr = new XMLHttpRequest();
        } catch (e) {
            try {
                xhr = new XDomainRequest();
            } catch (e) {
                try {
                    xhr = new ActiveXObject('Msxml2.XMLHTTP');
                } catch (e) {
                    try {
                        xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    } catch (e) {
                        statusField('\nYour browser is not' +
                    ' compatible with XHR2');
                    }
                }
            }
        }
        xhr.withCredentials = true;
        xhr.open('GET', mUrl, true);
        xhr.setRequestHeader("Authorization", "numberOfBLObsSent");
        xhr.send();
    };
</script>

And all the different ways getting failed. 并且所有不同的方式都失败了。 Please help me. 请帮我。

Thanks in advance. 提前致谢。

The issue is related to the cross-domain nature of the request. 该问题与请求的跨域性质有关。 When you make a cross-domain request which contains custom headers, the request is first "preflighted" to the server via the OPTIONS method, and the server must respond with a header Access-Control-Allow-Headers: your-custom-header . 当您发出包含自定义标头的跨域请求时,该请求首先通过OPTIONS方法“预检”到服务器,并且服务器必须以标头Access-Control-Allow-Headers: your-custom-header响应Access-Control-Allow-Headers: your-custom-header Once this is received, the ajax client will then (automatically) issue the actual request. 一旦接收到此消息,ajax客户端将(自动)发出实际请求。

More on preflighted requests 有关预检请求的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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