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/
Related
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.
On my website I've got an jquery accordion plugin. In order to collect all form data (from several forms) I'm using this line: $('#form_values').val($('form').serialize()). Then I submit the data to my php file, which in this case contains:
<?php
header('HTTP/1.1 500 Internal Server Booboo');
header('Content-Type: application/json; charset=UTF-8');
return json_encode(array("error" => "bad table name"));
?>
I would expect that my json/ajax/jquery script would display an alert box with "bad table name", but instead it just displays an alertbox with the text: Parseerror.
My jquery script:
$.ajax({
type:'POST',
data:$('#form_values').val($('form').serialize()),
url:'handle.php',
dataType: "json",
success: function(data){
alert("sucesso: "+data.msg);
window.location='index.php';
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert (textStatus);}
});
I haven't got a clue what's going wrong here.. hopefully someone is willing to help me out.
You need to use serializeArray instead of serialize and then convert it to a JSON string:
$.ajax({
type:'POST',
data:JSON.stringify($('form').serializeArray()),
url:'handle.php',
dataType: "json",
success: function(data){
alert("sucesso: "+data.msg);
window.location='index.php';
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert (textStatus);}
});
I think that somehow, the server script does not return json (use firebux/other debugging tools to check the response). As you specified that the response's type should be json, you get a parseerror.
Edit: are you sending the form to the right url? e.g. /handle.php instead of handle.php?
I'm trying to get echoed variable from php and display on the console. I'm using jquery to send data and I know the backend php file script is working fine. Since I want to see the progress and the backend php script echo out the iteration number while the script is running, I'm trying to see how I can get the echoed variable with jquery and display on the console.
I've tried success, onloading but it doesn't seem to work. Any help would be very appreciated. Thank you!
EDIT
$.ajax({
url: location,
type: 'POST',
data:{
iteration:"10",
readsize:"200",
slimdepth:"3",
sourcedirectory:source,
packagepath:MP
},
dataType:'json',
success: function (response) {
console.log("SUCCESS!");
console.log(response);
},
onInteractive:function (response) {
console.log(response.responseText);
},
complete:function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log("Failure!");
console.log(response.responseText);
}
});
So this is how I send the data and backend php script gets the parameters without problems and it runs. Now I can't see anything until php is done running and then it echo everything. In the backend php script, I have iteration number that I want to check and that is the problem since I can't check while it is running but only it is done running.
From what I could understand from your question probably this is what you are looking for.
You are sending data to PHP file with jQuery post or send:
$.post(file.php, {post}, function(data){
});
If you want to log returned data from php in console you must use console.log() function:
$.post(file, {post}, function(data){
console.log(data)
});
and now data will be logged into browser console.
$.ajax({
type: "POST",
url: "check-email",
data: {email: "name#example.com"},
success: function(data)
{
// success part here
},
error: function(xhr)
{
alert(xhr.status);
}
});
This will call another PHP file. But the sad thing is the error part is executed always with xhr.status printing as 0. What would be the problem?
Note:
1. This works perfectly in the local.
2. Both the script, check-email page is located in the same server.
3. The server is secured with https and it is in WWW version.
Update:
error: function(jqXHR, textStatus, errorThrown)
{
alert(textStatus, errorThrown);
}
This prints simply as "error".
I would have done this as a comment but the code would be a nightmare.
In your PHP page write this somewhere:
<?php
echo '<pre>'.print_r($_POST, true).'</pre>';
echo '<pre>'.print_r($_GET, true).'</pre>';
?>
Now:
Open up your Developer tools (Chrome) or Firebug (FireFox) and visit the Network tab.
Refresh your page that makes the AJAX call
Find the AJAX call to "check-email" in the Network tab and click it
What do you see?
Also there is a sub-tab called "Headers" in the developer tools, take a look at it
I copied this ajax call from another one that works and I can't figure out why it's not successfull. Here is the code:
$.ajax({
type: "POST",
url: "addTune.php",
data: {
database: setlist,
name: tune,
orderno: orderno,
midi: midi
},
error: function (e) {
alert("The PHP Call failed! hmmm");
alert(e.status);
},
success: function (response) {
alert(response);
}
});
I get the error function every time. Are there any blaring typos or other stupid mistakes?
Edit: trying to chase down the error with:
$.ajax({
type: "POST",
url: 'addTune.php',
data: {database : setlist, name : tune, orderno : orderno, midi : midi},
error: function(e){
alert("The PHP Call failed! hmmm");
alert(e.status);
$.ajaxSetup({
"error": function(jqXHR, status, thrownError) {
alert('error');
var responseText = jQuery.parseJSON(jqXHR.responseText);
console.log(responseText);
}
});
},
success: function(response){
alert(response);
}
});
});
Update: I was just able to add a row with the command line. any thoughts as to what I can do to try and narrow this down further?
i am no pro at this but maybe using single quote instead of double quotes? because i'm working on a project were i also have to do ajax calls and i use single quotes
i'm just suggesting something, not really good at this
If you get into the error callback then you have an error with the page you are calling and not with the js code. Check if the php page is reached. If it is check the php code if it returns a valid result.
From jquery DOCS.
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A
function to be called if the request fails. The function receives
three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a
string describing the type of error that occurred and an optional
exception object, if one occurred. Possible values for the second
argument (besides null) are "timeout", "error", "abort", and
"parsererror". When an HTTP error occurs, errorThrown receives the
textual portion of the HTTP status, such as "Not Found" or "Internal
Server Error." As of jQuery 1.5, the error setting can accept an array
of functions. Each function will be called in turn. Note: This handler
is not called for cross-domain script and JSONP requests. This is an
Ajax Event.