0

私はC#で簡単なセルフホストを作成します。ここで:

public JObject Get()   
{          
    jsonOut = @"{""server"": ""10.0.0.1"" }";
    return JObject.parse(jsonOut);
}

URL http://localhost:2000/api/testを Web ブラウザーで開こうとすると、応答 json: {"server": "10.0.0.1" } が正しく表示されます。Web ページを作成した後:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdlocalhost">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>test</title>
    <meta name="description" content="Portale per Webmaster">
    <meta name="keywords" content="HTML,CSS,JavaScript,PHP,ASP">
    <meta http-equiv="refresh" content="900">
    <script type="text/JavaScript">

        function ajaxRequest() {

            var xmlhttp = new XMLHttpRequest();
            var url = "http://10.1.3.62:2000/api/test";

            xmlhttp.onreadystatechange = function() {

                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var json=eval("("+xmlhttp.responseText+")");
                    document.getElementById("info").innerHTML = json.server;
                }
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }

        window.onLoad = ajaxRequest();
        setInterval(ajaxRequest, 2000);
    </script>
</head>
<body>
    <div id="info"></div>
</body>
</html>

しかし、変更は見られず、Firefox の開発ツールでは次のエラーが発生します。

SyntaxError: JSON.parse: JSON データの行 1 列 1 で予期しないデータの終わり

なぜ機能しないのですか?

ありがとう

4

1 に答える 1

0

私の提案は、JSON 形式を読み取るために AJAX を使用することです。次のようにコードを書き直します。

            var senderData = { id: "1" }

            $.ajax({
                type: "POST",
                url: "http://10.1.3.62:2000/api/test",
                processData: false,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(senderData),

                success: function (response) {

                    var items = eval(response.d);


                },
                error: function (error) {
                    console.log(error);
                }

            });
于 2016-03-30T15:22:48.410 に答える