Sencha touch 如何使用Sencha Touch 2在服务器的PHP文件中执行请求
我编写了一个移动应用程序,它使用SenchaTouch2框架展示了一些文章。 我在localhost和Wamp服务器中使用MySQL数据库。该应用程序运行良好。现在,我想从另一台具有特定IP地址的服务器部署我的数据库管理,但在Google Chrome上的JS控制台中有以下消息:Sencha touch 如何使用Sencha Touch 2在服务器的PHP文件中执行请求,sencha-touch,sencha-touch-2,Sencha Touch,Sencha Touch 2,我编写了一个移动应用程序,它使用SenchaTouch2框架展示了一些文章。 我在localhost和Wamp服务器中使用MySQL数据库。该应用程序运行良好。现在,我想从另一台具有特定IP地址的服务器部署我的数据库管理,但在Google Chrome上的JS控制台中有以下消息: XMLHttpRequest cannot load http://<ip_address>:[port]/[path_to_services_directory]/ArticleListService.p
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.
XMLHttpRequest无法加载
http://:[port]/[path_to_services_directory]/ArticleListService.php?action=read&_dc=1372077575445&keyword=&page=1&start=0&limit=25。起源http://localhost 访问控制允许原点不允许。
尽管如此,我还是在数据库配置中传递了良好的信息(主机、数据库名称、密码等)
以下是我的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
}
});
Ext.define(“largussumobile.store.ArticleListStore”{
扩展:“Ext.data.Store”,
初始化:函数(){
},
要求:[“largussumobile.model.ArticleModel”],
配置:{
模型:“Largussumobile.model.ArticleModel”,
代理:{
键入:“ajax”,
api:{
读取:http://:///ArticleListService.php?action=read”
},
外部参数:{
关键词:“
},
读者:{
键入:“json”,
rootProperty:“文章列表”,
totalProperty:“总计”
}
},
自动加载:正确
}
});
My 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();
?>
您的问题来自于尝试在与您的应用程序运行的服务器不同的服务器上执行php文件,这是正确的。唯一的解决方案是使用JSON-p代理:
另一个可能的解决方案是,如果您使用Sencha Touch制作本机应用程序,您可以使用类似PhoneGap(Cordova)的东西来允许这样的跨域请求。好的,我会尝试使用它。谢谢您的回答。再见。