简体   繁体   中英

Can't parse json format to ajax

I tried all the ways but still can't easily parse a json back to 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.

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. If response is in valid JSON format, parse it first, then read the keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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