2014-12-02 66 views
0

我有一個小腳本,它將運輸成本從PHP/CodeIgniter返回到HTML中。Ajax和json_encode在Firefox中回顯文本而不是json

這裏的PHP代碼,與陣列的一些示例數據:

public function atualiza_frete_ajax() { 

    $response_array = array(
     'html_select_frete' => "<form><select><option></option></select></form>" 
      );     
    header("Content-Type: application/json", true); 
    echo json_encode($response_array); 
    } 

而且JS/jQuery的

 function atualizar_frete_ajax() { 
      var $form = $("#form-cep"); 
       $(".loading").fadeIn(); 
       $.ajax({ 
        type: $form.attr("method"), 
        url: $form.attr("action"), 
        dataType: "json", 
        data: $form.serialize() 
        }).done(function(data){ 
         $(".frete-valor").html(data.html_select_frete) 
         $(".loading").hide() 
        }); 
       event.preventDefault(); 
     } 

現在的問題是:它在Chrome的偉大工程,但在Firefox我只是得到這個令人討厭的文本輸出。

這裏的FF如何輸出它的PRINTSCREEN:http://prntscr.com/5cfcgc

我已確認該文件的編碼是UTF8並沒有BOM,和我使用的是正確的標題之前,我附和響應。

任何線索?

+0

可以包括一個小的屏幕捕獲? – morne 2014-12-02 13:52:35

+1

您使用的是哪個版本的FireFox和jQuery? – MonkeyZeus 2014-12-02 13:55:00

+0

你有舊的FireFox版本嗎?還要注意'event.preventDefault();'在這裏沒用。 – 2014-12-02 13:55:35

回答

1

這不起作用,因爲你沒有阻止默認行爲。 event.preventDefault()是對的,但你需要正確使用它。如果你想要做眼前這個AJAX請求的形式提交你應該使用這樣的事情:

$(document).on('submit', '#form-cep',function(e) 
    { 
     e.preventDefault(); 
     var $form = $(this); 
      $(".loading").fadeIn(); 
      $.ajax({ 
       type: $form.attr("method"), 
       url: $form.attr("action"), 
       dataType: "json", 
       data: $form.serialize() 
       }).done(function(data){ 
        $(".frete-valor").html(data.html_select_frete) 
        $(".loading").hide() 
       });   
    }); 

也與該approuch你不會得到Firefox的錯誤了。

編輯

如果你還需要它,你可以使用這個功能:

function atualizar_frete_ajax() 
{ 
    var $form = $("#form-cep"); 
     $(".loading").fadeIn(); 
     $.ajax({ 
      type: $form.attr("method"), 
      url: $form.attr("action"), 
      dataType: "json", 
      data: $form.serialize() 
      }).done(function(data){ 
       $(".frete-valor").html(data.html_select_frete) 
       $(".loading").hide() 
      }); 
} 


$(document).on('submit', '#form-cep',function(e) 
{ 
    e.preventDefault(); 
    atualizar_frete_ajax(); 
});  
+0

不錯,不過要注意的一點是:我必須把它作爲腳本不同部分的函數=/ – 2014-12-02 16:02:11

+0

更新了我的asnwer,如果你需要它的話,可以使用類似的東西。 – 2014-12-02 16:08:50

+0

工作得很好!還有一個問題:有什麼不同: '$(document).on(「submit」,「#form-cep」,function(e){'和 '$(「#form-cep」)。submit(function(e){'? – 2014-12-02 16:20:31

相關問題