简体   繁体   English

JSON 调用返回 [object Object] 或未定义

[英]JSON call returns [object Object] or undefined

Very beginner level question.非常初级的问题。 Trying to learn JSON and having trouble with getting proper return data.尝试学习 JSON 并且无法获得正确的返回数据。 I'm trying to use JSON to return errors from a php form using json_encode($errs, JSON_FORCE_OBJECT) , but first I want to make the very simple code below work to understand JSON Objects.我正在尝试使用 JSON 从 php 表单中返回错误,使用json_encode($errs, JSON_FORCE_OBJECT) ,但首先我想让下面的非常简单的代码能够理解 Z0ECD10814C1D7A287ABB74A。

I've viewed similar threads here, read the entire JSON.org documentation, looked at JQuery's $.getJSON() method and plugged in the exact code from the documentation, as well as the exact code for the JSON documentation, and I still can't seem to get a response other than "undefined" or "[object, Object]".我在这里查看了类似的线程,阅读了整个 JSON.org 文档,查看了 JQuery 的 $.getJSON() 方法并插入了文档中的确切代码,以及 JSON 文档的确切代码,我仍然可以除了“未定义”或“[object, Object]”之外,似乎没有得到响应。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!---CSS-->
    <link href="style/960.css" media="all" rel="stylesheet" type="text/css" />
    <link href="style/pubstyle.css" media="all" rel="stylesheet" type="text/css" />
    <!--- Javascript -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" 
    type="text/javascript"></script>
    <script src="javascripts/form.js" type="text/javascript"></script>
    <script src="javascripts/validate.min.js" type="text/javascript"></script>
    <script src="javascripts/json2.js" type="text/javascript"></script>
    </head>
    <body>
    <script type="text/javascript">
         $(document).ready(function() {
             var resp = {"message":"hi"};
         $("a#button").click(function(resp) {
           alert(resp)
         });
         });

    </script>

    <a id="button" href="#">Button</a>
    </body>
    </html>

The code above returns an alert with "[object Object]".上面的代码返回带有“[object Object]”的警报。 When I change the code to:当我将代码更改为:

    $(document).ready(function() {
             var resp = {"message":"hi"};
         $("a#button").click(function(resp) {
           alert(resp.message)
         });
         });

I get an "undefined" alert.我收到“未定义”警报。 I tried putting it into an array object我尝试将其放入数组 object

{ "messages": [ {"message":"hi"}]}

and calling it with并用

resp.messages[0].message

with no luck.没有运气。 I tried change the Content-Type to "application/json" and "text/plain" and neither worked.我尝试将 Content-Type 更改为“application/json”和“text/plain”,但都没有奏效。 Also tried也试过

$(document).ready(function() {  
var resp = jQuery.parseJSON('{"message":"hi"}');
$("a#button").click(function(resp) {
alert(resp.message)
});
});

You're confusing a couple different elements here.您在这里混淆了几个不同的元素。

Simple example, based on the last code chunk in your question:简单示例,基于您问题中的最后一个代码块:

$(document).ready(function() {  
    var resp = $.parseJSON('{"message":"hi"}');
    $('#button').click(function(event) {
        alert(resp.message);
    });
});

Your version of this code didn't work because the click callback function receives the click event object as its argument.您的此代码版本不起作用,因为click回调 function 接收点击事件 object作为其参数。 The resp argument name in the callback shadowed the resp variable.回调中的resp参数名称隐藏了resp变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM