Calling ajax function returned error

I have following code for ajax on html button click.

$(document).ready(function(){
  $('#button').click(function() {
     //alert($("#data").serialize());
     $.ajax({
           type:"post",
           url:"geofenceupdatedata.php",
           data:  $("#data").serialize(),
           success: function(data){
                alert("Success");               
           },
            error:function(XMLHttpRequest, textStatus, errorThrown) { 
                alert("Status: " + textStatus.Message+errorThrown+XMLHttpRequest); 
                //alert("Error: " + errorThrown); 
            }
     });
  });
});

The PHP code:

<?php
// receive data from app's http request

include_once'./connectionusers.php';
echo var_dump($_POST);
$name=$_POST["value1"];
$lat=$_POST["value2"];
$latitude=(double)$lat;
$long=$_POST["value3"];
$longitude=(double)$long;
$rad=$_POST["value4"];
$radius=(double)$rad;
$message=$_POST["value5"];
$username="admin";
    $date=date("%c");

// write data from my webapp to a text file
file_put_contents('update.txt',$name." \n".$message);  

    $result=pg_query("UPDATE geofence  SET latitude='$latitude', longitude='$longitude', datetime='$date',         message='$message',        username='$username', radius='$radius' WHERE name='$name';");
 $js=json_encode(array("result"=>"success"));
 echo $js;
 return $js;
?>

The ajax code calls the php code and updates the database but the success part of ajax is not called. The error function is. Why would it be doing this?

Answers


You should add

  dataType: "json"

Remove echo var_dump($_POST); and return $js; from php file.


Need Your Help

Changing cell data

php grid radphp rpcl

For a project I need to work in RadPHP XE2

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.