我正在开发一个phonegap应用程序,我需要将一些数据从html文件发送到服务器并获得响应输出。 当我在保存服务器中拥有所有文件时,它们可以工作。当我将文件拆分为客户端和服务器机器时,他们不工作,据我所知,它与CROSS DOMAIN POST REQUEST有关。 以下是我的代码:
客户端: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();
}); });
服务器端: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();
}
我如何允许CROSS DOMNAIN POST,我知道我必须编辑我的服务器php文件,我已经添加了以下代码,但我的工作要么
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;
}
使用Firebug我说我的$ .POST,这就是我有
responseHeaders响应 RequestHeaders Content-Type application / x-www-form-urlencoded;字符集= UTF-8 接受 /
Parametersapplication / X WWW的窗体-urlencoded 密码asd 用户名qwe 资源 用户名= QWE&安培;密码= ASD
响应
响应始终为空白。它适用于所有html和php都在localhost或我的测试服务器中。 我的php和jquery技能缺乏atm所以发现它很难理解,如果其他人已经问过这个问题我很抱歉重复。 Ty为你提供帮助
答案 0 :(得分:0)
我必须做出的改变: 客户端:something.html
$(document).ready(function() {
$("#login").click(function(e){
var formData = $("#loginForm").serialize();
var username;
var passcode;
username= document.getElementById('username').value;
passcode = document.getElementById('passcode').value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var txt;
txt=xmlhttp.responseText;
//alert(txt);
localStorage.setItem("background", txt);
document.location.href="logged.html";
}
}
xmlhttp.open("GET","http://testserver.bscheme.com/rss/login.php?username="+ username +"&passcode="+ passcode,true);
xmlhttp.send();
});
});
服务器上的文件更改:Login.php
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
include('inc/db.php');
try
{
$userName = mysql_real_escape_string($_GET['username']);
$passcode = mysql_real_escape_string($_GET['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();
}
我所要做的就是确保服务器接受所有类型的标头,客户端发送XMLHttpRequest并返回html响应而不是$ .Ajax();