I am developing a phonegap application where i need to send a some data from a html file to a server and get a output in response. When i have all my files in save server they work. When i split the files into client and server machines they dont work and as far i understood it has to do with CROSS DOMAIN POST REQUEST. Here is my code below:
Client side: something.html
$(document).ready(function() {
$("#login").click(function(e){
var formData = $("#loginForm").serialize();
$.ajax({
type: "POST",
url: "http://testserver.bscheme.com/rss/login.php",
crossDomain: true,
cache: false,
data: formData,
success: function(txt){
if(txt !=""){
localStorage.setItem("background", txt);
//alert(localStorage.getItem("background"));
document.location.href="logged.html";
}
else
alert("Badly Failed");
}
});
e.preventDefault();
}); });
Server Side: login.php
<?php
include('inc/db.php');
try
{
$userName = mysql_real_escape_string($_POST['username']);
$passcode = mysql_real_escape_string($_POST['passcode']);
if(empty($feedName)!="" && empty($feedUrl)!="") {
$result = mysql_query("SELECT id,email,password FROM user WHERE email = '".$userName."'");
$row = mysql_fetch_array($result);
//echo $userName . $row['email'];
// echo $passcode . $row['password'];
if($row['email']== $userName && $row['password'] == $passcode && $row['email']!= "" && $row['password'] != "") {
echo $row['id'];
}else{
echo "fail";
}
} else {
echo "All fields are mandatory!";
}
}
catch(Exception $e)
{
// normally we would log this error
echo $e->getMessage();
}
How do i allow CROSS DOMNAIN POST, i know i have to edit my server php file and i have added the following code but i dint work either
switch ($_SERVER['HTTP_ORIGIN']) {
case 'http://localhost/rss': case 'https://localhost/':
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
break;
}
Using Firebug i say my $.POST and this is that i have
ResponseHeaders RequestHeaders Content-Type application/x-www-form-urlencoded; charset=UTF-8 Accept /
Parametersapplication/x-www-form-urlencoded passcode asd username qwe Source username=qwe&passcode=asd
Response
The response is always blank. It works when all the html and php is in the localhost or in my test server. My php and jquery skills are lacking atm so finding it very hard to understand also if someone else has already asked the question i am sorry for repeating. Ty for you help