www.Text返回整个html页面而不是数据

时间:2016-04-26 10:18:37

标签: c# php html unity3d

我有一个场景,我必须从php获取数据到我的Unity3d c#脚本。当我调用www.text时,我得到整个html页面代码。在Php页面我只有echo数据。

void Start() {
        StartCoroutine(GetText());
    }

    IEnumerator GetText() {
        UnityWebRequest www = new UnityWebRequest("http://192.18.23.1/php/Time.php");
        www.downloadHandler = new DownloadHandlerBuffer();
        yield return www.Send();

        if(www.isError) {
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            //Debug.Log(www.downloadHandler.text);
            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
            txt.text = "Success # " + www.downloadHandler.text;
        }
    }

修改

Time of Time.php

<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
    <script src="mqttws31.js" type="text/javascript"></script>


    <script>
    function myFunction(p1, p2) {
    return p1 * p2;   
    };
     var mqtt,payload;
     var value = 10;
    var reconnectTimeout = 2000;
    function MQTTconnect() {
    if (typeof path == "undefined") {
        path = '/mqtt';
    }
    mqtt = new Paho.MQTT.Client(
            'broker',
            1883,
             "/mqtt",
            "a:" + "abcdef" + ":" + Date.now()
    );
        var options = {
            timeout: 3,
            useSSL: false,
            cleanSession: true,
            onSuccess: onConnect,
            onFailure: function (message) {
                $('#status').val("Connection failed: " + message.errorMessage + "Retrying");
                setTimeout(MQTTconnect, reconnectTimeout);
            }
        };

        mqtt.onConnectionLost = onConnectionLost;
        mqtt.onMessageArrived = onMessageArrived;

        //if (username != null) {
            options.userName = 'username';
            options.password = 'password';
        //}

        mqtt.connect(options);

    }

    function onConnect() {


        // Connection succeeded; subscribe to our topic
        mqtt.subscribe('iot-2/type/+/id/+/evt', {qos: 0});
        $('#topic').val('iot-2/type/" + "+" + "/id/" + "+" + "/evt');

    }

    function onConnectionLost(response) {
        setTimeout(MQTTconnect, reconnectTimeout);
        $('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting");

    };

    function onMessageArrived(message) {

        var topic = message.destinationName;
         payload = message.payloadString;        
        //document.getElementById("ws").value = payload;    
    };
    </script>
</head>
    <body>
       <?php  
        echo '<script type="text/javascript">document.write(MQTTconnect());</script>';;
        $ff = $_GET['payload'];
        echo $ff;
        ?>  
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

可能最好的方法是使用服务器端php代码来生成输出,不包括所有html / javascript代码。

此外,您需要删除任何html标记并将内容类型用作文本,以确保解决方案正常运行。

例如:

<?php     
    header("Content-Type: text/plain");
    echo 'result';
?>