0

i create a simple selfhost with c#, where:

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

When i try to open a url http://localhost:2000/api/test into web browser, correctly see response json: {"server": "10.0.0.1" }. After i create a web page:

<!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>

but i don't see any changing and in development tools in firefox get the following error:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

why doesn't work?

Thanks

1 Answers1

0

My suggestion is to use AJAX in order to reading JSON format, I rewrite the code as follows:

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

            });
Amir
  • 125
  • 1
  • 6