IE9的AJAX响应包含无效字符

时间:2014-09-30 17:17:43

标签: javascript php jquery ajax json

我在IE9中得到了一个截然不同的JSON响应,并且不确定事情在哪里崩溃:

我的JS(这是使用jquery form- http://malsup.com/jquery/form/):

var self = this,
                options = {
                    beforeSubmit: this.beforeSubmit,
                    uploadProgress: this.uploadProgress,
                    success: this.uploadSuccess,
                    resetForm: true
                },
                form = $(this.forms.addNewDocument);

        $(form).ajaxSubmit(options);

我正在uploadSuccess内记录回复。

在我的PHP中,在使用json_encode之前,我得到的回应是:

array(2) {
 ["values"]=>
 array(1) {
   ["categoryId"]=>
   string(1) "1"
 }
 ["template"]=>
 array(1) {
   ["body"]=>
   string(211) "<li id="18"><span class="doc-title">test4.txt</span>&nbsp;<span class="doc-    date">9/30/2014</span>&nbsp;<span class="doc-user">Ling Chow</span><i class="fa fa-download"></i><i class="fa fa-times-circle"></i></li>"
 }
}

然后当我在json_encode($myresult)中包装数组并记录ajax响应时,我得到以下内容:

In IE 9
 {"values":{"categoryId":"1"},"template":{"body":"<li id='\"6\"'><span class='\"doc-    title\"'>test.txt&lt;\/span&gt;&nbsp;<span class='\"doc-date\"'>9\/30\/2014&lt;\/span&gt;&nbsp;<span class='\"doc-user\"'>Ling Chow&lt;\/span&gt;<i class='\"fa' fa-download\?="">&lt;\/i&gt;<i class='\"fa' fa-times-circle\?="">&lt;\/i&gt;&lt;\/li&gt;"}}</i></i></span></span></span></li>

In IE10/Chrome
{"values":{"categoryId":"1"},"template":{"body":"<li id=\"9\"><span class=\"doc-title\">accredmaster2.html<\/span>&nbsp;<span class=\"doc-date\">9\/30\/2014<\/span>&nbsp;<span class=\"doc-user\">Ling Chow<\/span><i class=\"fa fa-download\"><\/i><i class=\"fa fa-times-circle\"><\/i><\/li>"}} 

IE9正在添加大量单引号,&#34;?=&#34;在那里,看起来它早就结束了JSON。为什么会发生这种情况,为什么特定于IE9?

1 个答案:

答案 0 :(得分:0)

在php中header('content-type: application/json');之前使用json_encode。这将让IE9意识到你正在回归json并且不会试图逃避一切。