0

私のPCにはローカルApache 2.2サーバーがあります。システムで PHP 5 と MySQL 5.0 を使用しています。任意の文字列を含む 1 つの列と 1 つの行を持つ html5 という名前の MySQL データベースにテーブルを作成しました。

MySQL データベースに接続し、上記のテーブルの情報を抽出して XML ファイルとして返す sample.php という PHP ページを作成しました。

<?php   
    if(!$dbconnect = mysql_connect('localhost', 'root', 'password')) {
       echo "Connection failed to the host 'localhost'.";
       exit;
    } // if
    if (!mysql_select_db('mysampledb')) {
       echo "Cannot connect to database 'mysampledb'";
       exit;
    } // if

    $table_id = 'html5';
    $query = "SELECT * FROM $table_id";
    $dbresult = mysql_query($query, $dbconnect);

    // create a new XML document
    $doc = new DomDocument('1.0');

    // create root node
    $root = $doc->createElement('note');
    $root = $doc->appendChild($root);

    // process one row at a time
    while($row = mysql_fetch_assoc($dbresult))
    {
        // add node for each row
        $occ = $doc->createElement($table_id);
        $occ = $root->appendChild($occ);

        // add a child node for each field
        foreach ($row as $fieldname => $fieldvalue)
        {
            //create child element in xml file
            $child = $doc->createElement($fieldname);
            $child = $occ->appendChild($child);

            //add database content into the child element created above
            $value = $doc->createTextNode($fieldvalue);
            $value = $child->appendChild($value);

        }// foreach
    }// while

    // get completed xml document
    $xml_string = $doc->saveXML();
    echo $xml_string;
?>

ボタンをクリックするだけで上記の PHP ページに簡単なリクエストを送信し、応答を取得してアラートとして表示しようとする HTML5 ページを別のディレクトリに作成しました。

これは HTML5 ページです: -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>          
        <button onclick="myFunc()">Click me</button>
        <script type="text/javascript">
            function myFunc()
            {               
                $.ajax(
                    {
                        url:"http://localhost/sample.php"+"?randVar="+Math.random(),
                        type: "GET",
                        dataType: "text/xml",
                        success:function(result)
                        {
                            alert(result);
                        }
                    });
            }
        </script>
    </body>
</html>

しかし、このページを実行すると、IE9 でのみ応答が返されます。Firefox と Chrome で、空のアラートが表示されます。

この方法を試す前に、XMLHttpRequest オブジェクトも使用してみましたが、その方法でも IE9 でしか応答が得られません。

function myFunc()
{
    var xmlhttp, xmlDoc;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    try
    {                   
        xmlhttp.open("GET","http://localhost/sample.php"+"?randVar="+Math.random(),false);
        xmlhttp.send();
        xmlDoc=xmlhttp.responseText;
    }
    catch(e)
    {
        alert(e.message);
    }               
    alert(xmlDoc);
}

Firefox では、catch ブロックは "Failure" を返し、Chrome ではアラートにXMLHttpRequest Exception 101と表示されます。

PS: -

  1. HTML ファイルを sample.php ページと同じ htdocs フォルダーに保存すると問題は解決しますが、サーバーが別のネットワーク上の別のコンピューターである現実の世界では不可能です。だから私はこの提案を探していません。

  2. ajax 呼び出しでは、async="true" または "false" を作成しても違いはありませんでした。

これは IE9 での応答です: - アラート メッセージ

ファイアーバグ: - ファイアーバグ

誰かが私に解決策を提供してもらえますか? ひどく必要です。

4

3 に答える 3

1

かなりのリソースの後、私はこの解決策を見つけました:

ヘッダーを追加しています('Access-Control-Allow-Origin: *'); PHPファイルで私の問題を美しく解決しています。

皆さんの努力に感謝します!

礼儀: Origin は Access-Control-Allow-Origin によって許可されていません

于 2013-01-22T10:42:23.977 に答える
0

データ型を変更して試してください。

サポートのみ:dataType (default: Intelligent Guess (xml, json, script, or html))

$.ajax(
                {
                    url:"http://localhost/sample.php"+"?randVar="+Math.random(),
                    type: "GET",
                    dataType: "xml",
                    success:function(result)
                    {
                        alert(result);
                    }
                });

参照:http ://api.jquery.com/jQuery.ajax/

于 2013-01-22T06:49:22.170 に答える
0

次のヘッダーを php ファイルに配置します。

header( 'Content-Type: text/xml, charset=utf-8' ); 
于 2013-01-22T06:32:50.623 に答える