2013-06-25 58 views
-3

嗨,现在我在我的页面中使用$.post方法,但现在我想使用async:false属性,我无法将它与$.post一起使用。所以我需要将$.post转换为$.ajax方法。我是新来的阿贾克斯你能帮我吗?

from_date = $('#from').val(); 
to_date = $('#to').val(); 
$.post('test.php',{from:from_date,to:to_date},function(res){ 
    $("#ro").html(res); 
    ro_name = $('#ro').val(); 
}); 

哪里#ro是格在那里我张贴上述调用的结果的ID,但是这是降下来了。在页面加载我想先下降到另一篇文章的方法来从数据库中获得的结果通过,但由于异步调用它的到来null所以我想用$.ajax方法,而不是$.post

+1

阅读http://api.jquery.com/jQuery.ajax/ –

+1

使用文档,这不是很难找到相同的。顺便说一句,同步使用ajax实在是一个坏主意。相反,使用你的ajax($ .post)请求的回调方法,并根据它编写你的逻辑。 –

回答

0

看到这个jQuery AJAX

你的代码可以改变

$.ajax({ 
    type: "POST", 
    url: url_destination, 
    data: {from:from_date,to:to_date}, 
    success: ffunction(res){ 
    $("#ro").html(res); 
    ro_name = $('#ro').val(); 
    } 
}); 
0

尝试像

$.ajax({ 
    url : 'test.php', 
    type : 'POST', 
    data : {from : from_date , to : to_date}, 
    success : function(res){ 
     $("#ro").html(res); 
     ro_name = $('#ro').val(); 
    } 
}); 
0

该文档是在这里:http://api.jquery.com/jQuery.post/

具体来说:

这是一个速记的Ajax˚F油膏,这相当于:

$.ajax({ 
    type: "POST", 
    url: url, 
    data: data, 
    success: success, 
    dataType: dataType 
}); 
0

这应该为你工作:

var from_date = $("#from").val(); 
var to_date = $("#to").val(); 

$.ajax(
{ 
    url: 'text.php', 
    type: 'POST', 
    data: { from: from_date, to: to_date }, 
    async: false, 
    success: function(res) 
    { 
     $("#ro").html(res); 
     ro_name = $('#ro').val(); 
    } 
}); 
0

Official documentation

$.ajax({ 
    type: 'POST', 
    async: false, 
    url: 'test.php', 
    data : { from: from_date , to: to_date }, 
    success : function(res){ 
    $('#ro').html(res); 
    ro_name = $('#ro').val(); 
    } 
})