Nice programing

jQuery로 Accept HTTP 헤더를 올바르게 설정할 수 없습니다.

nicepro 2020. 10. 24. 11:44
반응형

jQuery로 Accept HTTP 헤더를 올바르게 설정할 수 없습니다.


이 jquery 코드를 사용하여 Accept HTTP 헤더를 "text / xml"로 설정하려고합니다.

$.ajax({
    beforeSend: function(req) {
        req.setRequestHeader("Accept", "text/xml");
    },
    type: "GET",
    url: "[proper url]",
    contentType: "text/plain; charset=utf-8",
    dataType: ($.browser.msie) ? "text" : "xml",
    username: '---',
    password: '-------',                                
    success: function(data) {
        var xml;
        if (typeof data == "string") {
            alert("Data is string:" + data);
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.loadXML(data);
        } else {
            xml = data;
            alert("Data is not string:" + $(xml).text());
        }
        // Returned data available in object "xml"
        //alert("Status is: " + xml.statusText);
        $("#ingest_history").html($(xml).text());
    }              
});

파이어 폭스에서는 훌륭하게 작동합니다.

그러나 IE에서는 Accept 헤더에 대해 설정하려는 값이 끝에 추가되어 다음과 같이 Accept: */*, text/xml됩니다. 이로 인해 내 ajax 호출이 내가 원하는 xml 버전과 반대로 html 버전을 반환합니다.

아무도 IE 8에서 Accept 헤더를 올바르게 설정 / 지우는 방법을 알고 있습니까?

업데이트 됨 : 어떤 이유로 입력 할 때 별표가 나타나지 않았습니다. IE의 수락 헤더는 다음과 같습니다 Accept: */*, text/xml.


IE뿐만 아니라 jQuery 1.6.2를 사용하는 Chrome 및 Safari에서도 문제가 발생했습니다. 이 솔루션은 내가 시도한 모든 브라우저 (Chrome, Safari, IE, Firefox)에서 의도 한대로 작동하는 것으로 보입니다.

$.ajax({
    headers: { 
        Accept : "text/plain; charset=utf-8",
        "Content-Type": "text/plain; charset=utf-8"
    },
    data: "data",
    success : function(response) {
        ...
    }
})

이것이 여전히 문제를 일으키는 경우 시도하십시오.


jQuery 1.5 이상을 사용하면 수락 헤더를 설정할 dataType수 있으므로 다음과 같이 할 수 있습니다.

$.ajax({
    dataType: ($.browser.msie) ? "text" : "xml",
    accepts: {
        xml: "text/xml",
        text: "text/xml"
    }
});

귀하의 문제는 http://www.grauw.nl/blog/entry/470에 설명 된 문제인 것 같습니다 . 문제는 XMLHttpRequest 사양에 현재 사용자 에이전트가 요청에 대해 기본적으로 Accept 헤더를 설정하지 않아야한다고 명시되어 있으므로 req.setRequestHeader ()가 새 Accept를 추가 할 수 있습니다. 불행히도 브라우저는 아직이를 준수하지 않습니다. 문제 작성을 통해 브라우저가 제대로 작동하는지 테스트 할 수 있으며, 안타깝게도 IE7, Chrome, Safari, Firefox 및 Opera는 모두 실패합니다.

Laurens Grauw는 또한 먼저 Accept 헤더를 무효화하려고 시도한 결과에 대해 이야기합니다.

setRequestHeader('Accept', '')

또는

setRequestHeader('Accept', null)

여기에 도움이 될 수 있습니다.

Ugly server-side hacks: If you have control over your server-side app you can hardwire it to always return XML, add support for a custom media type like "application/i-really-want-xml", or add support for a custom HTTP header like "X-Accept".


I think the original poster might have been referring to this link: http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx however, this doesn't explain the behavior you see.

IE does not, by itself, have the behavior you describe, and setting the Accept header via XMLHTTPRequest should work properly. I've tested in IE8 to confirm.

It's possible there's an issue in your version of jQuery, or perhaps you have some plugin mangling your traffic?


Although this isnt how the documentation states it needs to be done, it is what worked for me.

 jQuery.ajax({
    type: "POST",
    url: "...",
    data: ...,
    contentType: "text/xml",
    beforeSend: function(req) {
    req.setRequestHeader("Accept", "text/xml");
    },  ...});

I don't believe that IE(any version) plays nice with the Accept header. See this link: [http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx]

A possible solution might be to check the User Agent to see if it's IE. If it is, then check for the presence of text/xml.

Good Luck!

Edit:

Opps on the link. My hunch was IE is always adding the / and setting the accept header just adds the desired mime type after the /.

참고URL : https://stackoverflow.com/questions/1145588/cannot-properly-set-the-accept-http-header-with-jquery

반응형