我编写了一个移动应用程序,它使用Sencha Touch 2 Framework提供了一些文章。 我在localhost中使用带有Wamp服务器的MySQL数据库。该应用程序运行良好。现在我想从具有特定IP地址的其他服务器部署我的数据库管理,但我在Google Chrome的JS控制台中有以下消息:
XMLHttpRequest cannot load
http://<ip_address>:[port]/[path_to_services_directory]/ArticleListService.php?action=read&_dc=1372077575445&keyword=&page=1&start=0&limit=25. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
然而,我传入了我的数据库配置良好的信息(主机,db_name,密码等)。
这是我的ArticleListStore.js定义:
Ext.define("LargusDuMobile.store.ArticleListStore", {
extend: "Ext.data.Store",
initialize: function () {
},
requires: ["LargusDuMobile.model.ArticleModel"],
config: {
model: "LargusDuMobile.model.ArticleModel",
proxy: {
type: "ajax",
api: {
read: http://<ip_address>:<port>/<path_to_services_directory>/ArticleListService.php?action=read"
},
extraParams: {
keyword: ""
},
reader: {
type: "json",
rootProperty: "articles_list",
totalProperty: "total"
}
},
autoLoad: true
}
});
我的ArticleListService.php代码:
<?php
include_once "../db/DummyDbManager.php";
include_once "../db/PDODbManager.php";
include_once "AbstractService.php";
class ArticleListService extends AbstractService
{
//Properties
private $m_name = NULL;
private $m_dbManager = NULL;
private $m_callback = NULL;
//Singleton instance
static private $m_instance = NULL;
//Initialization
private function __construct(&$dbManager)
{
$this->m_dbManager = $dbManager;
$this->m_name = get_class($this);
}
//Singleton pattern
static public function getSingleton($dbManager)
{
if (self::$m_instance == NULL)
self::$m_instance = new ArticleListService($dbManager);
return (self::$m_instance);
}
//Read the 15 most recent articles
public function read(array &$ref_result)
{
$ref_result = array("articles_list" => array(), "total" => 0);
$query = "SELECT a.id, a.title, a.contents, a.main_image FROM argus_articles a"
. " WHERE a.contents NOT LIKE '%<iframe%' ORDER BY a.id DESC LIMIT 15 OFFSET 200";
$dbresult = $this->m_dbManager->query($query);
while ($row = $this->m_dbManager->fetch($dbresult)) {
array_push($ref_result["articles_list"], array(
"id" => $row["id"],
"title" => addslashes((string)$row["title"]),
"contents" => (string)$row["contents"],
"main_image" => (string)$row["main_image"]));
}
$ref_result["total"] = $this->m_dbManager->affectedRows();
$this->m_dbManager->disconnect();
}
//Create a new record
public function create(array &$ref_result)
{
}
//Update a specific record
public function update(array &$ref_result)
{
}
//Destroy a specific record
public function destroy(array &$ref_result)
{
}
//Initialize action dispatcher function pointer
public function initialize_action_dispatcher(&$action_map)
{
$action_map = array("read" => "read", "create" => "create",
"update" => "update", "destroy" => "destroy");
}
//Request launcher
public function launch()
{
$this->m_dbManager->connect();
if (isset($_REQUEST["action"])) {
$action_dispatcher = array();
$this->initialize_action_dispatcher($action_dispatcher);
$html_result = array();
$this->m_callback = $action_dispatcher[$_REQUEST["action"]];
call_user_func_array($this->m_name . '::' . $this->m_callback, array(&$html_result));
}
header("Content-Type: application/x-json");
$json_result = json_encode($html_result);
echo $json_result;
}
}
//Entry point
function main()
{
$configFile = "../config/config.xml";
$dbManager = new PDODbManager($configFile);
$blogService = ArticleListService::getSingleton($dbManager);
$blogService->launch();
}
main();
?>
我认为问题来自于我从Wamp服务器执行我的应用程序并且我想在另一个上执行外部PHP文件(因此是跨域问题)。有人有这个问题吗? 非常感谢您的帮助。
答案 0 :(得分:0)
你是正确的,你的问题来自尝试在不同于运行应用程序的服务器上执行php文件。解决此问题的唯一方法是使用JSON-P代理:http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.proxy.JsonP
另一个可能的解决方案是,如果您使用Sencha Touch制作本机应用程序,则可以使用类似PhoneGap(Cordova)的内容来允许像这样的跨域请求。