0

我编写了一个移动应用程序,它使用 Sencha Touch 2 框架展示了一些文章。我在带有 Wamp 服务器的 localhost 中使用 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 文件(所以是跨域问题)。有没有人已经有这个问题?非常感谢您的帮助。

4

1 回答 1

0

您是正确的,您的问题来自于尝试在与您的应用程序运行位置不同的服务器上执行 php 文件。唯一的解决方案是使用 JSON-P 代理:http ://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.proxy.JsonP

另一种可能的解决方案是,如果您正在使用 Sencha Touch 制作本机应用程序,您可以使用 PhoneGap (Cordova) 之类的东西来允许这样的跨域请求。

于 2013-06-24T13:34:25.067 回答