1
我目前正在jquery上工作,我想使用ajax從API調用中獲取信息,我在模糊函數中獲取來自輸入的值,然後調用終點,但ajax調用保持在readystate 1,我不知道爲什麼。Ajax函數不工作readystate 1
這就是我所做的。
(function ($) {
$(document).ready(function() {
$(document).on("change","#calc_shipping_method",function(){
$('.rp_calc_shipping').trigger('click');
});
$('.shipping_postcode input,.shipping_state input').blur(function() {
$(".loaderimage").show();
element=$(this);
var datastring = $(this).closest(".woocommerce-shipping-calculator").serialize();
if($("input.variation_id").length>0){
datastring=datastring+"&variation_id="+$("input.variation_id").val();
}
if($("input[name=quantity]").length>0){
datastring=datastring+"¤t_qty="+$("input[name=quantity]").val();
}
var zip = $("#calc_shipping_postcode").val();
// THIS DOENS'T WORK
var state = $.ajax({
type: "GET",
crossOrigin: true,
url: "http://api.postmon.com.br/v1/cep/"+zip,
dataType: 'json',
success: function() {
alert ("hello world");
}
});
state = state['responseJSON']['estado'];
//03134-001
datastring.replace("calc_shipping_state=","calc_shipping_state="+state);
$.ajax({
type: "POST",
url: rp_ajax_url+"?action=update_shipping_method",
data: datastring,
success: function (data) {
$(".loaderimage").hide();
element.parent().parent().find('.shippingmethod_container').html(data);
}
});
return false;
});
});})(jQuery);
的事情是,在Ajax的工作原理的警報,但該對象的readyState爲1
通常,結果數據的處理是在成功方法中執行的。 'state = state ['responseJSON'] ['estado'];'看起來期待ajax以同步方式運行,而ajax默認不會運行。 – Taplar
對,所以,readystate是1.聽起來正確。 'state = state ['responseJSON'] ['estado'];'應該失敗,因爲尚未收到json。 –
我把異步:假和完美的工作,謝謝 –