Hi all is it possible to call a an ajax call without using success? ie from:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
success: function(msg){
//window.location.replace(msg);
}
});
to something simply like:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
});
To give reasoning - I have written some php to insert items into a database and then redirect to another page, I don't want to have to re-write the same code again to insert into the DB and then redirect in JS.
Hi all is it possible to call a an ajax call without using success? ie from:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
success: function(msg){
//window.location.replace(msg);
}
});
to something simply like:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
});
To give reasoning - I have written some php to insert items into a database and then redirect to another page, I don't want to have to re-write the same code again to insert into the DB and then redirect in JS.
Share Improve this question asked Apr 2, 2014 at 12:37 TotalNewbieTotalNewbie 1,0245 gold badges13 silver badges25 bronze badges 10- 3 Have you tried your second code? – dkasipovic Commented Apr 2, 2014 at 12:39
- 2 Sending a redirect in server-side code that's accessed using an XMLHttpRequest generally doesn't result in the actual web page changing to the new location. – Anthony Grist Commented Apr 2, 2014 at 12:40
- sure guess so Ill give it a go - was just assuming it wouldn't work – TotalNewbie Commented Apr 2, 2014 at 12:40
- 2 @TotalNewbie, It tends to be better practice to try first before asking if something will work. – Emily Shepherd Commented Apr 2, 2014 at 12:42
- it doesn't work - cheers for the downvotes – TotalNewbie Commented Apr 2, 2014 at 12:43
2 Answers
Reset to default 4jQuery.post()
documentation
$.post( "/project/test/auto", data );
I don't think that you can redirect using PHP within ajax call.
It would be best to create a function out of DB insert and then with ajax call a page that executes that function and returns success:true in json for example. After that, redirect in success part of your ajax call. You can later call the same DB insert function from within your PHP code.