2016-03-30 9 views
0

JavaScriptでの応答を、読ん{ "サーバー":" 10.0.0.1 "}。のJSON selfhostのC#ので出力し、私は、C#で簡単なselfhostを作成

<!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:行でのデータの予想外の終わりを私は、Webページを作成した後 1列目のJSONデータ

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

おかげ

答えて

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); 
       } 

      }); 
関連する問題