Reputation: 2289
I have this code
$(document).delegate('#login', 'pageinit', function(event) {
console.log('inside login page')
$('#loginform').submit(function() {
// Get the value of the username and password
var myusername = $("#username").val();
var mypassword = $("#password").val();
// Post to the login route
$.post(global_urlstub + '/customer_login', {username: myusername, password: mypassword}, function(data) {
alert(data);
console.log(data);
if (data.flag == true) {
alert('123');
console.log(data);
jQuery.mobile.changePage('#page1');
}
else {
alert('12345');
console.log(data);
$('#errmsg_login').html(data.msg);
}
}, "json" );
return false;
});
});
My server returns hash with key flag
. However this code returns me no data to console or alert , while post request is successful. What am i doing wrong?
Upvotes: 8
Views: 15222
Reputation: 12624
Make sure that you take any debugging output code in your server side script.
Also, using Firebug or chrome tools, you can easily check out the response from the server to see if it is in valid JSON format.
Upvotes: 0
Reputation: 1474
I would agree with Hemant_Negi that this is a problem with you server not returning valid JSON. The following code based on yours works fine (change of URL in there too which returns valid JSON):
var myusername = 'a', mypassword = 'b';
$.post('http://ip.jsontest.com', {username: myusername, password: mypassword}, function(data) {
alert(data.ip);
console.log(data);
if (data.flag == true) {
alert('123');
console.log(data);
jQuery.mobile.changePage('#page1');
}
else {
alert('12345');
console.log(data);
$('#errmsg_login').html(data.msg);
}
}, "json" ).fail( function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
});
If you change that URL, the call to fail() should alert an error message.
Upvotes: 3
Reputation: 2078
It happens when you server is not returning a valid JSON
try it without datatype first like below..
$.post(global_urlstub + '/customer_login', {username: myusername, password: mypassword}, function(data) {
alert(data);
console.log(data);
if (data.flag == true) {
alert('123');
console.log(data);
jQuery.mobile.changePage('#page1');
}
else {
alert('12345');
console.log(data);
$('#errmsg_login').html(data.msg);
}
});
Upvotes: 4