我想发送一个普通的get请求,它像普通的get请求一样替换整个页面但是如果可能的话使用jQuery。我还需要在请求中发送2个参数。
我不想用结果替换文档中的某些内容,结果就是完整的文档。
这里的代码仍然发送了一个ajax请求,我的页面没有刷新响应..
$.get({
url: '/swap_games',
data: { source: sourceElem, target: targetElem }
});
答案 0 :(得分:3)
jQuery有一个load()
方法可以帮到你。
.load( url, [data,] [complete(responseText, textStatus, XMLHttpRequest)] )
示例如下:
$('html').load('/swap_games', data: { source: sourceElem, target: targetElem } );
或者使用回调:
$('html').load('/swap_games', data: { source: sourceElem, target: targetElem }, function() {
alert('load complete callback');
});
更多信息here
答案 1 :(得分:1)
$.get({
url: '/swap_games',
data: { source: sourceElem, target: targetElem }
success: function( data ){
$('html').html( data );
})
});
答案 2 :(得分:0)
您需要传入第三个替换内容的函数参数。
$.get('/swap_games', { source: sourceElem, target: targetElem }, function(data) {
$('html').replaceWith(data);
})
答案 3 :(得分:0)
我没有足够的声誉点来评论已接受的答案,但应该注意的是,当数据作为对象提供时,load()方法会执行POST请求,就像在示例中一样。接受了答案。如果提供了字符串,则执行GET请求。
还应该注意,load()和$ .get()方法都是异步的,可能不需要你的情况。您可能正在寻找的是
window.location.href = "http://your_url_with_any_query_parameters";
不需要jquery。