简体   繁体   English

AJAX jQuery 在 IE8 上成功,在 IE10/Chrome/FF 上失败

[英]AJAX jQuery success with IE8, fails with IE10/Chrome/FF

I am facing a very odd issue with jQuery and AJAX.我在使用 jQuery 和 AJAX 时遇到了一个非常奇怪的问题。 My AJAX call goes to success with IE8 but into error with IE10, Chrome and Firefox.我的 AJAX 调用在 IE8 上success ,但在 IE10、Chrome 和 Firefox 上error

Here is the PHP code in Zend Framework 2:以下是 Zend Framework 2 中的 PHP 代码:

$response = $this->getResponse();
$headers = $response->getHeaders();
$headers->clearHeaders()
        ->addHeaderLine('Content-Type', 'application/json; charset=utf-8');
$response->setContent(json_encode($array));
return $response;

Where $array is a PHP array. $array是一个 PHP 数组。

Here is my Javascript code:这是我的 Javascript 代码:

$.ajax({
    url: "/application/getTypeDossierParPilote/" + idPilote,
    type: "POST",
    async : false,
    success: function(data) {
        LISTE_TYPE_BANDEAU = data;
        ID_PILOTE_SELECTED_BANDEAU = idPilote;
        LISTE_TYPE_BANDEAU_LOADED_BANDEAU = true;                    
    },
    error: function errorHandler(e)             
        ID_PILOTE_SELECTED_BANDEAU = 0;
        LISTE_TYPE_BANDEAU_LOADED_BANDEAU = false;
        LISTE_TYPE_BANDEAU = null;                      
    }                
});

When using IE8, it goes to success and the data are usable.使用IE8时, success ,数据可用。 When using IE10, it goes to error .使用 IE10 时,它会error Although, when my client tries this on IE10, he doesn't get any issue, so I assume it goes straight to success .虽然,当我的客户在 IE10 上尝试这个时,他没有遇到任何问题,所以我认为它会直接success

I have tried adding something like dataType: "json", and even dataType: "html", to check whether the JSON is correct or not and it goes to success in that latter case but the data are not usable.我尝试添加类似dataType: "json",甚至dataType: "html",以检查 JSON 是否正确,在后一种情况下它会success ,但数据不可用。

When I use the developer mode of the browser to copy the response body and check on http://jsonlint.com/ whether the JSON is correct or not I get the following message :当我使用浏览器的开发人员模式复制响应正文并检查http://jsonlint.com/ JSON 是否正确时,我收到以下消息:

JSON.parse: unexpected character at line 1 column 1 of the JSON data JSON.parse:JSON 数据第 1 行第 1 列的意外字符

where the first character is the curly bracket.其中第一个字符是大括号。 Here is an example of a JSON that I get :这是我得到的 JSON 示例:

{
    "typeDossier": [
        {
            "type": "MED",
            "famille": "AEC",
            "codeType": 1,
            "session": ""
        },
        {
            "type": "HPS",
            "famille": "AEC",
            "codeType": 2,
            "session": ""
        }
    ],
    "aecHps": true,
    "CodeAecHps": "1,2,",
    "tous": "1,2,"
}

Hope you can help me out with this weird issue.希望你能帮我解决这个奇怪的问题。 Thank you in advance.先感谢您。 Regards,问候,

My problem has been fixed and was caused by BOM characters which appeared in the configuration file of my project.我的问题已修复,是由我的项目配置文件中出现的 BOM 字符引起的。

Check here to see how I fixed it: Web server response generates UTF-8 (BOM) JSON在这里查看我是如何修复它的: Web 服务器响应生成 UTF-8 (BOM) JSON

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

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