Ajax request returns failure - php

I have written a small script to make an Ajax request. I use jQuery for this purpose. I have done this several times but this is the first time I get a failure message. This is my script:
$(document).ready(function(){
$("#btnExecute").click(function(){
var numberOfClusters = $('#inputNumCab').val();
alert(numberOfClusters);
$.ajax({
type: "POST",
url: 'RunPythonThroughPHP/insertData_2.php',
//data: { numberOfClusters: numberOfClusters },
success: function(msg){
alert('thanks');
},
error: function(){
alert("failure");
}
});
});
});
The concept is the following. When I press the button (which is in a form) I get the value of a text field (based on id) and I pass this data into my ajax request. The problem is I get the failure alert.
Why this happens? If I make my request not async (async: false) then it works.

Related

Yii2 abort request and sql query

After page fully loaded, i make ajax request to an action.
While waiting for response from action (it takes 2 or 3 seconds), if user clicks on other link, i want to abort previous request and kill mysql process at once.
How can i do this?
I tried to do like this
var xhr = $.ajax({
/*
params
*/
});
//before unload page
xhr.abort();
but i think, that it will not kill sql process.
Make a named ajax. Here is ajx is named
var formData = {};//Your data for post
var ajx = $.ajax({
type: "POST",// Http verbs
url: "filename.php",// file to request
data: formData,
success: function(response){
console.log("ajax completed");
console.log(response);
}
});
Stop ajax request by clicking button
$( "#button_id" ).click(function() {
ajx.abort();
});

How to solve Firebug’s “Aborted” messages when sending form data via AJAX?

I have created a PHP web application and use MySQL as database backend, but there is an 'Aborted' note on Firebug's Net panel's status column when I access the web page. Why?
$('#submit').on('click', function () {
// e.preventDefault();
var formData = JSON.stringify($("#frmPayoye").serializeObject());
console.log(formData);
$.ajax({
type: "POST",
url: "http://www.sanaroopay.com/pg/api/ectransact/",
data: formData,
cache: false,
timeout: 60000,
async: false,
processData: true,
dataType: 'json', //you may use jsonp for cross origin request
contentType: "application/json; charset=utf-8",
crossDomain: true,
success: function (data) {
alert(JSON.parse(data));
// alert("ok");
console.log("success");
// window.location.assign('https://secure.payoye.net/web/merchant');
},
error: function () {
console.log("Failed");
}
});
});
You are not cancelling the form submission so the Ajax call is aborted and the page submits the form as it is designed to do. So you need to stop the form submission.
$('#submit').on('click', function (evt) {
evt.preventDefault(); //stop the default action of the button
//Rest of your code
});
Please see the documentation of XHR open() for example here: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest
Note: Calling this method an already active request (one for which open()or openRequest()has already been called) is the equivalent of calling abort().
Just create a new XHR instance whenever you need one. Better yet, use jQuery or other JS library to do AJAX. It should shield you from these intricacies.
How to solve Firebug’s “Aborted” messages upon Ajax requests?

javascript array to php array

i am sending data through ajax call to the php code my ajax code is this
var values = JSON.stringify({ dstring: dataString, ukey:ukey });
var page_path = server_url+"save_data.php";
$.ajax({
type: "POST",
url: page_path,
cache: false,
data: values,
dataType: "json",
success: function(msg){
},
error:function(xhr, status, error) {
}
});
and in the ajax it send data like this
{"dstring":{"q2":"11","q3":"22","q4":"33","q5":"44","q6":"55"},"ukey":"1"}
and in the php when i try to get it through REQUEST it dont show me data , i am bit confuse on how to handle this data in php
Don't stringify data on your ajax call. You should then be able to $_POST['dstring']on the PHP script. Also, you should add in some debug code at least into that error handler to know what's up. Last but not least, inspect the network calls.
You have to get file_get_contents("php://input") and run that through json_decode.

PhoneGap jQuery POST not working

Good evening,
I was trying a long time with different syntaxes but haven't got it working:
Everything is running on PhoneGap. I get the console_log before $.ajax.., but not any error or output after it.
After click on "Submit" this JS code is executed:
console.log(in_mail + " " + in_text);
$.ajax({
type: 'POST',
data: "mail="+in_mail+'&text='+in_text,
url: 'http://example.com/comment.php',
success: function(data){
console.log(data);
alert('Your comment was successfully added');
},
error: function(){
console.log(data);
alert('There was an error adding your comment');
}
});
console.log("POST done");
Two things:
If you are executing this script via submit button you should consider returning true on success and false on error.
Your "POST DONE" would execute before the success or error functions would be invoked because post is asynchronous.
I have reproduced the example with jsFiddle you can find it here http://jsfiddle.net/jUBMN/

How to get multiple responses from PHP file via AJAX?

My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email.
What I want to do is to show status messages via ajax. For example, "First operation done, please wait for second" and then when second one will be finished show the next message "Second operation done too". Now my ajax looks like that.
How can I modify it?
//add status data to form
form.data('formstatus', 'submitting');
if (validate()) {
//send data to server for validation
$.ajax({
url: formUrl,
type: formMethod,
dataType: "json",
data: formData,
success: function (data) {
$.notifyBar({
cls: data.status,
html: data.message
});
form.data('formstatus', 'idle');
}
});
}
in the success block you can perform another ajax call. That's the simplest. You can do it to in .success(), .ajaxSucces(), .complete(), or .then() function like this: $.ajax(...).success(...);
ideally you would embed the code in a function, by example
$.ajax({
url: formUrl,
type: formMethod,
dataType: "json",
data: formData,
success: function (data) {
notifyResponse(data);
form.data('formstatus', 'idle');
sendMail();
}
});
function sendMail() {
$.get(mailUrl, function(data) { // or $.post(...
notifyResponse(data);
});
}
function notifyResponse(data) {
$.notifyBar({
cls: data.status,
html: data.message
});
}
If you've to do two operations that have different execution times, just send two different AJAX queries, and get the responses from them.
Divide your PHP service in two parts. If the second part depends on the first, instead of sending the two requests at the same time, send the second request when the first one returns.
In other words, in your success callback, you're going to notify the user that the first operation has been completed and you proceed to call the second operation, whose success callback will inform that the second operation has been completed.

Categories