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。 –
我把异步:假和完美的工作,谢谢 –