簡體   English   中英

通過javascript / jQuery訪問嵌套JSON對象時出現問題

[英]Issues accessing nested JSON objects via javascript/jQuery

這是我得到的JSON:

{"status":"failure","msg":{"name":["can't be blank"],"email":["can't be blank","is invalid"]}}

這是我正在使用的javascript:

$("#sign_in_form").submit(function() {
  var success_function = function(data) {
    if(data.status == 'failure') {
      $("#error_msg").html(data.msg.name)
    }
  };

  $.post($(this).attr("action"), $(this).serialize(), success_function, "json");
});

我從Firebug中得到的錯誤是:

uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://localhost:3000/assets/jquery.js?body=1 :: <TOP_LEVEL> :: line 6182" data: no]

一切正常。 我的JavaScript可以訪問data.status值,但使用data.msg.name會產生未捕獲的異常。

嘗試

data.msg.name[0]

希望能有所幫助

它應該是

$("#error_msg").html(data.msg.name[0] || '');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM