1

I have an array that looks like this: {"errors":1, "login_success": false} What I want to do is to separate these two values: 1 and false so that I can do some checks with jquery and so on.

Here's my jquery:

   hash = Math.random();     
$.ajax({
  type: "POST",               
  url: "java/jquery/AjaxLogin.php",
  data: "hash="+hash+"&username="+username+"&password="+password+"&remember_user="+remember_user,
  beforeSend : function(){  
     $("#loading_image").show();
     $("body").css("cursor", "wait");
  },
  error : function(XMLHttpRequest, textStatus, errorThrown){
     error = 1; 
  },
  success : function(data) {                    
     $("#loading_image").hide();
     $("body").css("cursor", "default"); 
    alert(data);

     $.each($.parseJSON(data), function(idx, obj) {
        suc = obj.login_success;
        er = obj.errors;

        alert(er);

    });


     // data tells whether login ok or not
     if(data == -10) {
        error = 1;
     } else if(data == -1) {
        error = 2;
     } else if (data > 0) {
        // go to homepage
        url = "http://intra.tobiasfransman.net/index.php";
        $(location).attr('href',url);
     }

  }
 });

But for some reason I can't get two values separated, where are I'm going wrong?

Toube
  • 57
  • 8
  • 1
    `$.parseJSON(data)` why do you need to parse it? isn't it already a proper json? try with `data` only without parsing. – Jai Nov 28 '14 at 12:45

2 Answers2

1

This parsing is a little wonky. In the below method you are taking the string, turning it into a Javascript object, then iterating every entry in the object, and each time you assign the .login_success and .errors to the suc and er values. If you had 10 items in your response object, you would do the assignment 10 times. You're also throwing away your Javascript object, when you probably want to keep it around for interrogation later.

 $.each($.parseJSON(data), function(idx, obj) {
    suc = obj.login_success;
    er = obj.errors;

    alert(er);

});

What you should do is this.

 dataObject = $.parseJSON(dataString);

 //Now you can access the values off of the dataObject with Javascript dot notation. I have no idea what you want to do with the following code, so I just made some stuff up as an example.
 if(dataObject.errors == -10) {
    error = 1;
 } else if(dataObject.errors == -1) {
    error = 2;
 } else if (dataObject.loginSuccess > 0) {
    // go to homepage
    url = "http://intra.tobiasfransman.net/index.php";
    $(location).attr('href',url);
 }

Keep in mind that if your web server returns something with status code 400 or greater than you will be in the error handling of your ajax call.

error : function(XMLHttpRequest, textStatus, errorThrown){
     error = 1; 
  },

Import Caveat - JQuery does some magic for you. If the Content-Type header of the HTTP response coming back is a correct json content type (What is the correct JSON content type?), then jQuery will automatically call parseJSON on it, and you should not call parseJSON a second time inside of your success/error handler. If the server is not responding with the correct Content-Type, but you have knowledge that it will always respond with JSON in the body, you can force this behavior by setting the dataType in the settings object passed into the $.ajax() call.

Community
  • 1
  • 1
Jazzepi
  • 4,722
  • 6
  • 49
  • 73
  • Ok, I want to alert only one value lets say the login_success = false? Now as you said it always alerts all the found values. How do I do this? – Toube Nov 28 '14 at 12:58
  • @user2023042 Delete your for loop. It's unnecessary. Interact with the data object directly either before or after parsing it. I'm not sure what content type you're getting back on your call so I can't tell you if you need to use parseJSON() or not. data.login_success, and data.errors – Jazzepi Nov 28 '14 at 12:59
  • Hi, I deleted the for loop but I still alerts both values in json array – Toube Nov 28 '14 at 13:12
  • Jazzepi, without parsing it alerts undefined – Toube Nov 28 '14 at 13:18
  • Okay so you definitely need to do the parsing which is fine :) But what do you mean it alerts both values? Can you show your new code? I'm not sure what you've written after the initial question. You can edit it and add the new code. – Jazzepi Nov 28 '14 at 13:22
  • My bad I had 2 alerts now it works great work, thanks – Toube Nov 28 '14 at 13:30
  • Glad I could help! Good luck on your project! – Jazzepi Nov 28 '14 at 13:50
-1

var data = {"errors":1, "login_success": false};

$.each(data, function(idx, obj) {
    suc = data.login_success;
    er = data.errors;

    alert(er);
    alert(suc);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

you should access data directly

$.each($.parseJSON(data), function(idx, obj) {
    suc = this.login_success;
    er = this.errors;

    alert(er);

});

UPDATE #1

$.each(data, function(idx, obj) {
    suc = data.login_success;
    er = data.errors;

    alert(er);
    alert(suc);

});

This works for you!

Telmo Silva
  • 325
  • 1
  • 5
  • Thanks for quick reply, that returns undefined – Toube Nov 28 '14 at 12:41
  • Use `obj.login_success` and `obj.errors`. In the each: `The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.` – vaso123 Nov 28 '14 at 12:46
  • Still returns undefined alert 4 times – Toube Nov 28 '14 at 12:55