简体   繁体   English

无法将json格式解析为ajax

[英]Can't parse json format to ajax

I tried all the ways but still can't easily parse a json back to ajax. 我尝试了所有方法,但仍然无法轻松地将json解析回ajax。 either I can't get the response value or the object length is too long (when i put breakpoint to troubleshoot). 我无法获取响应值或对象长度太长(当我将断点用于故障排除时)。 only that response works when I tried "return jsonify(status[0])", but I don't think it's a good way to list out all into jsonify. 当我尝试“返回jsonify(status [0])”时,只有该响应有效,但我认为这不是将所有内容都列出到jsonify中的好方法。

url:"/Verify_form",
              type:"post",
              contentType:'application/json',
              dataType: "json",
              processData:false,
              async: false,
              success:function(response){
                       if (response == "success"){
                          alert("[Success] - Verify Form !!!" );
                          action = 2;} 
                        else if (response["failed"]) {alert(response["msg"])}
                        else  {
                              var len = Object.keys(response).length;
                              var selects = document.querySelectorAll("select[id=select_host]");
                              if (len == 1) {
                                for (var index = 0; index < selects.length ; index++)
                                if (selects[index].value == response["hostname"])
                                BLINK(stage[index]);
                              }
                              else if (len > 1){
                              for( var ind = 0; ind < len; ind++){
                                for (var index = 0; index < selects.length ; index++)
                                  {
                cur=g.db.execute('update table set status=(\'%s\') where hostname=(\'%s\')'
                %("error",i))
                g.db.commit()

            cur=g.db.execute('select hostname from table where mac_address <> \'\' AND status=(\'error\') ')
            status = [dict(hostname=row[0]) for row in cur.fetchall()]
            app_json = json.dumps(status)
            ## return jsonify(app_json) ## Error - len in break point equal 37  
            ## return jsonify(status[0]) ## success


Console the ajax response and see in all cases (success,failed ..), weather it is proper JSON format or not. 控制台ajax响应,并在所有情况下(成功,失败..)查看其是否为正确的JSON格式。 If response is in valid JSON format, parse it first, then read the keys. 如果响应采用有效的JSON格式,请先对其进行parse ,然后再读取密钥。

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

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