无法将json格式解析为ajax

时间:2019-07-10 15:41:28

标签: javascript json flask

我尝试了所有方法,但仍然无法轻松地将json解析回ajax。 我无法获取响应值或对象长度太长(当我将断点用于故障排除时)。 当我尝试“返回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


1 个答案:

答案 0 :(得分:0)

控制台ajax响应,并在所有情况下(成功,失败..)查看是否使用正确的JSON格式。 如果响应的格式为有效的JSON,请先parse,然后再读取密钥。

相关问题