Ajax post not working - Jquery Phonegap Android Ajax post not working - Jquery Phonegap Android android android

Ajax post not working - Jquery Phonegap Android


Finally, after 4-5 days searching..something worked out for me..I just added return false after the ajax callback. And i changed the code as following:

$('form').submit(function(e){           if (password_ok==1 && email_ok==1)            {                var postData = $(this).serialize();                $.ajax({                    type: 'POST',                    url: 'http://mysite.xx.x/smartphone/login.php',                    dataType : 'json',                    data: postData,                             success: function(data){                        alert(data);                    },                    error: function(){                        alert('error!');                    }                });            }        else            {            e.preventDefault();            if (email_ok==2)                {                $("#1").css("border-color","red");                $("#1").css("background-color","#FFD8CE");                }            if (password_ok==2)                {                $("#2").css("border-color","red");                $("#2").css("background-color","#FFD8CE");                }            }    return false;           });

Now the only problem is that it does not reset the page when successful..but one step at time...


Step 1 :You need to include two headers in your php file

header('Content-Type: application/json');header("Access-Control-Allow-Origin: *");

Step 2 :And include cdn jquery library in your index page.

step 3 :use script to post the form successfully

<script type="text/javascript">    $(document).ready(function() {        $('form').submit(function(e) {            var postData = $(this).serialize();            $.ajax({                type : 'POST',                url : 'http://www.yoururl/test.php',                dataType : 'json',                data : postData,                success : function(data) {                    alert(data);                },                error : function() {                    alert('error!');                }            });            return false;        });    });</script>

I have tested it in phonegap and it is working fine.


I think the reason is caused by "Form submission". Where did you put your ajax code? In the index.js?

The exception you got means "0 == Page is unloading."

In your case, i guess you put your ajax codes in index.js which is in the same page with the form. When you submit the form, browser starts to unload current page and load page with results of form submitting. So your ajax codes get the exception "Page is unloading".

Try not to use Form submission.