This question already has answers here:
JavaScript to reload the page as GET request
(3 answers)
Closed 2 years ago.
I have this weird issue here where I have the following ajax call inside a file called index.php:
$.ajax({
method:'post',
data:
{
action: 'del_comment',
comment_id: comment_id,
},
success: function (data) {
location.reload(true);
}
});
The PHP portion of the page which intercepts the ajax request is the following:
if($_POST['action'] == 'del_comment') {
// Do some processing
die;
}
The issue is that the ajax is executed but when the page is reloaded, the request is a POST instead of a GET. I was under the impression that the following line in the success of my ajax call:
location.reload(true);
Should force a GET request, but it doesn't work. How can I reload the same page with a GET request after the ajax call?
User window.location instead of "location.reload(true);" with the get parameters that you want, you have to create custom URL with parameters and you can use it like (window.location = 'YOUR URL?GET PARAMETER=VALUE';)
Related
This question already has answers here:
How do I close a connection early?
(20 answers)
Closed 8 years ago.
okay so,
[browser] ==> makes AJAX call to webserver running php
i have the following php code,
<?php
//Process Ajax request
//return success code to browser
//continue with other php code
?>
How exactly can i achieve the second part i.e "return success code to browser"? and continue with processing php code.
put **async:false** in ajax call so your code will execute synchronously means until you don't get success or failure from ajax return rest of code will not execute .. do like following
// add here jquery file
<script>
$.ajax({
url : "abc.com/page",
type: "POST",
data: {},
async:false
success : function(data)
{
//you can use data here
}
});
</script>
//you can write here php code
i think it can be done like follow
//initial php code
call_function();
echo json_encode(array('success'=>true));
//other php code
call_function(); //etc
or you can split the ajax call in 2 parts.. in 1st part it will get the status so echo json_encode() will be executed and call ends and in success call back of ajax make 2nd call which will execute the remaining code!
This question already has answers here:
Abort Ajax requests using jQuery
(18 answers)
Closed 8 years ago.
I would like to stop an ajax call in jquery, which is not working
var xhr = null;
xhr = $.ajax({
url : 'www.example.com?some-large-call',
success : function(responseText) {
// some DOM manipulation
}
});
$("#button").click(function() { xhr.abort() });
I referred the below link
http://www.stoimen.com/blog/2009/09/29/jquery-stop-an-ajax-call/
this probally has more XHR..
Please see this answer:
jquery abort() ajax request before sending another
From the link:
every time you do ajax request, add to array:
requests.push(
$.ajax({
type: 'post',
url: '/test.php',
data: search_data,
success: function(data) {
$('#catalog').html(data);
}
}));
Then, when you want to stop calls..
loop through array and abort all calls..
for(var i = 0; i < requests.length; i++)
requests[i].abort();
Thanks
xhr.abort() it will cause the client to stop listening for the event, but probably it may not stop the server from processing it.
Provided the code you've pasted is the code you're actually using it's no wonder it doesn't work.
As it states on the site it is just pseudo code to illustrate an example. There is no AJAX request there and nothing to stop (at least nothing large enough to be able to stop)
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I'm new in the web programming and I don't fully understand some simple things.
Suppose we have following ajax request:
var records = [];
$.ajax(
{
url : "index",
dataType: 'json',
success: function (response)
{
records = response;
}
});
alert(records.length);//This displays 0
alert(records.length);//This alert correctly displays number of records
The problem is that the array records appears empty, if I try to use them immediately after calling ajax (first alert displays zero length, while second alert displays correct length). What's the problem and how to solve it?
You just need to put your alert inside the success callback.
var records = [];
$.ajax(
{
url : "index",
dataType: 'json',
success: function (response)
{
records = response;
alert(records.length); // will always be correct
}
});
In your example, the behavior will be unpredictable and will depend on how fast the call returns.
The A in Ajax stands for Asynchronous
The success function doesn't trigger until the HTTP response comes back.
When the first alert fires, it hasn't come back. In the time it takes for you to click OK to that alert, the response has arrived so the success function has run by the time the second alert fires. (alert is a blocking function).
Do your work on the data in the success function, not immediately after sending the HTTP request.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery AJAX submit form
I have a form on a page A, and instead of me posting to a new page, is there a way that i can do onSubmit ajax request it instead? I want to make the page to be able to load in the result into a div.
I know how to write the script for the call sort of, but i wasn't sure if i had to hand write it or it if was possible to take all the form information and send it without having to code it all into the attributes. i was thinking I could just do some sort of thing like: onClick, post form but return results into a div.
Thoughts? Ideas?
Try using JQuery's serialize (API ref here)
EDIT: something like this:
$("input.submit").click( function() {
$.ajax( {
type: "POST",
url: $("form").attr( 'action' ),
data: $("form").serialize(),
success: function( response ) {
//Code to process the response
}
});
return false;
} );
$.ajax({
url: 'mypage.html',
success: function(data){
$('.result').html(data);
},
error: function(){
alert('failure');
}
});
Then you have a div:
<div class="result"> </div>
This will automatically populate with the result of your ajax post.
http://api.jquery.com/jQuery.post/
Also see TONS of related questions: Jquery checking success of ajax post
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get URL parameter with jQuery
I have a form that posts data over ajax. The response comes back in the form of GET variables in the URL. For example if there was a failure of writing the form data the return address would be: http://example.com/?error=1
How would I check for this using jquery. Right now when I do a console.log on the msg variable I just get the html output of example.com (which I guess makes sense). I need the GET variables though. How would I achieve this?
$('#wp_email_capture').submit(function(e){
var email = $('#wp-email-capture-email').val();
$.ajax({
type: "GET",
url: "/",
data: "wp_capture_action=1&wp-email-capture-email=" + email
}).done(function( msg ) {
console.log(msg)
});
e.preventDefault();
});
AJAX by default will return the content from the request. What you want are the headers.
If you are being given a return address of http://example.com?error=1, then it means this is being returned as a redirect header.
To get the header information from an AJAX request, that has been answered here:
jQuery and AJAX response header