javascript - Ajax and json_encode echoing text instead of json in Firefox -
i have small script returns shipping costs html , php/codeigniter.
here's php code, example data array:
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); }
and 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(); }
now issue is: works great in chrome, in firefox nasty text output.
here printscreen of how ff outputs it: http://prntscr.com/5cfcgc
i've made sure file encoding utf8 , no bom, , i'm using correct header before echo response.
any clues?
it's not working because not preventing default behaviour. event.preventdefault()
right, need use correctly. if want ajax request @ moment form submitting should use this:
$(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() }); });
also approuch won't firefox error anymore.
edit
if need function can use this:
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(); });
Comments
Post a Comment