Cross-Domain Ajax call with JSNOP in PhoneGap


Client side script:


	var url = base_url;  // you'll want to change

	$.ajax({
		type: 'GET',
		url: url,
		contentType: "application/json",
		dataType: 'jsonp',
		crossDomain: true,
		data: {
			username: $('#username').val(),
			password: $('#password').val()
		},		
		beforeSend: function ( xhr ) {
			//show your image loader here
			$('.loader-image').fadeIn(100);
		},
		success: function ( res ) {
		    //hide your image loader
			$('.loader-image').fadeOut(100);
			if( res.value === 2 ) {
				//unsuccessful code goes here
				return false;
			}
			if( res.value === 1 ){
				//success code goes here
			}
		}
	});

Server side code:


$email = $_GET['username'];
$pass = $_GET['password'];

$user = DO user query using $email and $pass checking.

if (!empty($user)) {
	$value = 1;
} else {
	$value = 2;
}
$theName->value = $value;
echo $_GET['callback'] . '(' . json_encode($theName) . ')';	

2 responses to “Cross-Domain Ajax call with JSNOP in PhoneGap

Leave a comment