javascript - JSON 响应作为文件打开,但我无法使用 JavaScript 访问它

标签 javascript jquery json response

在下面的代码中,我向 servlet 发出一个 POST 请求,servlet 以这种方式回复:

response.setContentType("application/json");
json = "{success:true,sessionUid:\""+sessionUid+"\"}";
response.getWriter().write(json);

所以 Firefox 像打开文件一样打开它,我可以看到它没问题。这里有 JSON:

{success:true,sessionUid:"D07WC15R7LFRFRGPF4P5"}

问题是我无法检查 JSON 对象。它似乎不存在于我的回调函数中(也使用 Firebug)。查看代码和警报。

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#loginForm").submit(function(response){
alert("response="+response);    //output: "response=[object Object]"
                    var obj = jQuery.parseJSON(response);
alert("obj.sessionUid="+obj.sessionUid);    //doesn't work, Firebug says "obj is null"
                    if (response.success == true){ //never true
                        document.location.href = 'http://localhost:8080/QuoteroClient/logged.jsp';
                    }else{
                        alert("Something went wrong in the login process.");
                    }
                    return false;
                });
            });
        </script>
    </head>
    <body>
        <form id="loginForm" action="http://localhost:8080/QuoteroClient/Main?servlet=Security" method="post">
            <fieldset><legend>Login to Quotero:</legend>
                <label>Action:</label><input type="text" name="action" value="login"/><br />
                <label>Username:</label><input type="text" name="login-quotero" value="admin"/><br />
                <label>Password:</label><input type="text" name="password-quotero" value="admin" /><br />
                <label>Domain:</label><input type="text" name="combo-domain" value="Quotero" /><br />
            </fieldset>
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

编辑:我也尝试对 AJAX 请求执行相同的操作,但没有成功:

$("#ajaxSubmit").click(function () {
  $.ajax({
    type: "GET", //GET or POST is the same for this servlet
    url: "http://localhost:8080/QuoteroClient/Main?servlet=Security&action=login&login-quotero=admin&password-quotero=admin&combo-domain=Quotero",
    dataType: "json",
    success: function (response) {
alert("response=" + response);
      var obj = jQuery.parseJSON("" + response);
alert("obj.sessionUid=" + obj.sessionUid);
      if (response.success == true) {
        document.location.href = contextPath + 'http://localhost:8080/QuoteroClient/logged.jsp';
      } else {
        alert("Something went wrong in the login process.");
      }
    }
  });
  return false;
});

最佳答案

这不是有效的 JSON:

{success:true,sessionUid:"D07WC15R7LFRFRGPF4P5"}

这是有效的 JSON:

{"success":true,"sessionUid":"D07WC15R7LFRFRGPF4P5"}

在 JSON 中,键必须始终加引号。请参阅DEMO .

关于javascript - JSON 响应作为文件打开,但我无法使用 JavaScript 访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5180762/

相关文章:

javascript - 可搜索标签文本 Chartjs

javascript - 为 div 元素分配唯一的 id

jquery - 在调用重置后,如何在 btn 组中禁用按钮?

MySQL 选择 JSON 字段属性具有值的位置

javascript - Three.js drawImage 从多 View Canvas WebGLRenderer 绘制到多个 Canvas 上

javascript - 将元素添加到 Uint8ClampedArray 类型数组的快速方法

javascript - 正则表达式匹配错误数据

javascript - 如何使用 JQuery 的 parseXML 将转义字符串解析为 XML 对象?

java - 致命异常 : AsyncTask #1 when trying to access remote database from mobile android app

php - 将带有非数字键的 PHP 数组编码为 JSON 数组