【问题标题】:I want to post data and display the http response send by the server on the browser but i am stuck on browser nothing is displayed我想发布数据并在浏览器上显示服务器发送的 http 响应,但我卡在浏览器上,没有显示任何内容
【发布时间】:2012-11-06 09:05:12
【问题描述】:

我想发布数据并在浏览器上显示服务器发送的 http 响应,但我卡在浏览器上什么都没有显示

我通过 Wire Shark 检查了服务器正在发送 json 响应,但浏览器上没有显示任何内容,我想获取响应正文并对其进行解析,然后将其显示给用户。

有没有办法获取表单详细信息,然后将其包装在变量上并将其发布到服务器

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $.post(
                    "http://192.168.0.135:8080/uid",
                    {
                        "type" : "CREATE_ACCOUNT",
                        "data" : '{ "hardwareID" : "SAM1234567890123", "name" : "Jain", "emailID" : "jain@gmail.com", "password" : "dgbedbeifkfk" }'
                    },
                    function(data,status) {
                        alert("Data: " + data + "\nStatus: " + status);
                    }
                );
            });
        });
    </script>
</head>

<body>
    <button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>

【问题讨论】:

  • 事情是你可以发送任何 json 到服务器。你的问题是在 json 中发送 json ......就像数据有另一个 json 字符串......
  • 你有没有看过像 Firebug 这样的开发者工具,当你执行 $.post() 时会返回什么?
  • 我收到 JavaScript Object Notation: application/json 作为我的响应
  • 我来自服务器的响应是 Content-Type: application/json){ "data": {}, "type": "CHANGE_PASSWORD", "result": { "status": 401, "message ": "未经授权 - 会话 ID 无效。" }- } 这是 json 那么我如何从服务器获取此响应并将其显示到浏览器
  • 'data' 应该是那个对象..

标签: jquery html post http-post


【解决方案1】:

只是让您知道,您的 JSON 无效,这是您的 JSON 的正确版本:

{
    "type": "CREATE_ACCOUNT",
    "data": {
        "hardwareID": "SAM1234567890123",
        "name": "Jain",
        "emailID": "jain@gmail.com",
        "password": "dgbedbeifkfk"
    }
}

删除“数据”行中 { 前面的 '。

我强烈建议您在搜索其他错误之前通过jsonLint.com验证您的 JSON。

【讨论】:

  • 是的,我知道这不是问题....我要问的是如何显示 json(响应)
猜你喜欢
  • 2015-09-15
  • 2020-11-10
  • 2020-04-26
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 2016-10-10
相关资源
最近更新 更多