2012-11-16 29 views

回答

4

你可以,但这是不必要的。改为使用$.get方法直接访问响应。

$.get("foo.php",function(response){ 
    console.log(response); 
}); 

如果你想两者都做,(即加载的响应到一个div,并使用返回的数据),你可以使用回调的$.load方法相类似。

1

在这种情况下,使用jQuery的GET,POST或AJAX方法。
.load jquery方法内部只使用异步http请求,所以上面提到的所有方法。

4

根据docs

.load(url [, data] [, complete(responseText, textStatus, XMLHttpRequest)]) 

所以,你可以创建功能,可以节省你的回应:

$('#result').load('ajax/test.html', function(responseText, textStatus, request) { 
    alert(responseText); 
}); 

另一种方法是使用$.get$.post方法:

$.get('ajax/test.html', function(data) { 
    $('.result').html(data); 
    alert(data); 
}); 

$.post('ajax/test.html', function(data) { 
    $('.result').html(data); 
    alert(data); 
}); 
2
$(selector).load(url); 

仅仅是一个速记:

$.get(url, data, function(response) { 
    $(selecton).replaceWith(response); 
}); 
2

你可以尝试这样的事情在AJAX

$.post(yourfile,function(response) { 
    //div hidden with the html of your page 
     $("#hiddendiv").html(response); 
}); 

或与获得

$.get(yourfile,function(response) { 
    //div hidden with the html of your page 
     $("#hiddendiv").html(response); 
});