AJAX call without success field? - javascript

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.

jQuery.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.

Related

The ajax result is not returned

I'm trying to import MySQL data(select query) using spring, mybatis.
In js, call the controller function via ajax, and get DB data from the controller.
ex.
ajax
url: /testmysql
controller
requestmapping: /testmysql
return mav (modelandview)
sysout(mav) is good for the controller.
But Ajax is not found(404) in js.
I was told that an ajax 404 error comes out when there is no return value.
But what should I do now?
You are making ajax call without root context url of your application.
$.ajax({
url: '/context/testmysql',
type:'GET',
success: function(data) {
console.log(data);
}
});

jquery ajax isn't working on the correct order

I am trying to load from a PHP file using ajax, some JSON objects but my problem is that my javascriptcode is not working on the correct order. Here is my code:
var requests;
$(document).ready(function(){
setInterval(function(){
$.ajax({
url: "test2.php",
success: function(result){
requests = JSON.parse(result);
alert(requests);
}
});
})
});
alert(requests);
Here is the first alert i'm getting on page load and here is the second alert
My question is why the alert in the last line is executed before the ajax
Note: this is a small example of my project, my real error is that i cannot load some arrays i need using ajax, because it shows as undefined at console, even if the ajax is in the beginning of the script.
try this cod for ajax call with php
$.ajax({
type: "method", // post or get
url: "file url",
dataType: "json",
success: function(data){
},
error: function(){
}
});
The best way to avoid this is to create a function in which second alert would be placed. And call this function inside ajax call's success method.
e.g.
ajax({
...
success: function(){
...
secondAlert();
};
...
});
function secondAlert(){
alert("alert after jax call");
};
As for
My question is why the alert in the last line is executed before the ajax
AJAX is an acronym for Asynchronous JavaScript and XML. The keyword here is asynchronous. AJAX sends the HTTP request and keeps executing the code in async fashion. The callback function of the AJAX request is executed after the request is finished and the corresponding response has been received. More info is available here

Passing variables with URL not working quite right using PHP and ajax

I'm using AJAX to update a page every 5000 milliseconds. It works great but I have ran into one issue. When I try and get data that is in the URL using $_GET or $_POST it does not work. It instead returns a value of a 1. Here is some example code.
In main.php I have this:
$(document).ready(function worker() {
$.ajax({
url: 'Request.php',
type: 'POST',
success: function(data) {
$('#Live_data').html(data);
},
complete: function() {
setTimeout(worker, 5000);
}
});
})();
and when this is called it fires off the request.php. In request.php I have some code to grab what was added in the URL by a previous page but it dose not work. It goes something like this:
$value = $_get['test'];
This is supposed to return the value in the URL parameter test but it does not work.
Thanks!
You forgot to send data with the ajax query,
In this code, you can add GET data by append a query string to url value, or send POST data by setting data property of the request,
$.ajax({
url: 'Request.php?query=string&is=here',
type: 'POST',
data: {to: 'post', goes: 'here'},
success: function(data) {
$('#Live_data').html(data);
},
complete: function() {
setTimeout(worker, 5000);
}
});
see also https://api.jquery.com/jquery.post/#jQuery-post-settings
I've commented it as well but I'll post it as answer:
Change POST to GET in your jQuery AJAX request.
Use request.php instead of Request.php or the other way around.
Use $_GET instead of $_get. This variable is case sensitive.
Your not sending any data here. You can send the required data in Url or in Data Field.
url: 'Request.php?test=xyz',
or
data: data,

How do I post serialized data along with a jQuery.getScript() call?

Is it possible to request an external JS file while also posting serialized data in that same request? I'd like to pass along some values to validate the request, but not have those values be a part of the request url.
On the backend I'll process the posted values and return the proper JS after validation, then continue with the injected JS functions as a callback of getScript.
I see no data option in the API:
http://api.jquery.com/jQuery.getScript/
$.getScript is a shorthand Ajax function, which is equivalent to:
$.ajax({
url: url,
dataType: "script",
success: success
});
So I assume You can use the following code to do that
$.ajax({
url: url,
data: {data: serializedData},
dataType: "script",
success: success
});

jquery sending form data and a json object in an ajax call

I'm making a call to another ajax page, the call posts a json object.
I also need to send data from a form
(not using submit - I have the ajax call attached to a button which uses e.preventDeault()).
The call is as folows:
var myUrl = 'sendswatch-data.php';
$.ajax({
url: myUrl,
data: {'swatchid[]':swatchArray}, 'formdata':$('#orderData').serialize()},
type: "POST",
error: function(xhr, statusText, errorThrown){
// Work out what the error was and display the appropriate message
},
success: function(myData){
$('#tabsampleorder').html(myData);
$('.tabber').hide();
$('#tabsampleorder').show();
}
});
I have a form on the page id of formdata.
How do I send this as well as the json object? I've tried
data: {'swatchid[]':swatchArray}, 'formdata':$('#orderData').serialize()},
but that's generating an error.
You have an extra } after watchArray. Try removing that.
data: {'swatchid[]':swatchArray, 'formdata':$('#orderData').serialize()},
You can send data from the form as follows:
data : { swatchid: swatchArray, formdata: $('#orderData').serialize() }
You will need a parameter in the controller for every field that your add.

Categories

Resources