开发者

how to send a get request with params without ajax

开发者 https://www.devze.com 2023-04-11 19:46 出处:网络
I want to send an ordinary get request which replaces the entire page just like an ordinary get request but using jQuery if possible. I also need to send 2 parameters in the request.

I want to send an ordinary get request which replaces the entire page just like an ordinary get request but using jQuery if possible. I also need to send 2 parameters in the request.

I don't want to replace some content in my document with the result, the result is the complete document.

The code here st开发者_StackOverflow社区ill sent an ajax request and my page was not refreshed with the response..

$.get({
       url: '/swap_games', 
       data: { source: sourceElem, target: targetElem } 
});


jQuery has a load() method that does this for you.

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

Example as follows:

$('html').load('/swap_games', data: { source: sourceElem, target: targetElem } );

Or with a callback:

$('html').load('/swap_games', data: { source: sourceElem, target: targetElem }, function() {
   alert('load complete callback');
});

More info here


$.get({
       url: '/swap_games', 
       data: { source: sourceElem, target: targetElem } 
       success: function( data ){ 
          $('html').html( data ); 
       })
});


You need to pass in a third function parameter which replaces the content.

$.get('/swap_games', { source: sourceElem, target: targetElem }, function(data) {
        $('html').replaceWith(data);
})


I don't have enough reputation points to comment on the accepted answer, but it should be noted that the load() method performs a POST request when the data is supplied as an object, as it is in the example of the accepted answer. If a string is supplied, a GET request is performed.

It should also be noted that both the load() and $.get() methods are asynchronous and probably not needed for your case. What you were probably looking for is

window.location.href = "http://your_url_with_any_query_parameters";

No jquery needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号