简体   繁体   中英

split ajax json response errors in each field

i used till now AJAX post with normal dataType 'html' , now i convert it and using dataType 'json' ,

Now before i used json i split response errors with for each span next to the input field .

this is my old ajax succses split errors code using normal datatype 'html' :

    success:
           function(data){

  var data_array = split('*');
                 for(var i=0; i<data_array.length-1; i++)
                 {
                    messgae_array = data_array[i].split(':');
                    $("#"+messgae_array[0]+"_error").html(messgae_array[1]);
                    $("#"+messgae_array[0]).css({"border":"1px solid red"});
                 }

}

this is my input on the form

<input type="text" id="uname" name="uname" value="" class="inplaceError" />
<span id="uname_error"></span>

i have more inputs i puted just ex how it look..

so every input i have span that holds the errors from the ajax response.

and now the problem is my old code know handle html response and not json response , i am javascript/ajax/json newbie , and i dont know how to change the split code so work with json response and not normal html response , any help please?

Edit:

i fire the errors like this :

   if(isset($_POST['p']) && !empty($_POST['p']))
    {
        if($password == $retype)
            {
            $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
        // Create salted password (Careful with the chilli)
        $password = hash('sha512', $password.$random_salt);

        }
        else
        {
          $message['password']='&nbsp;password not match';
        }

and so on .

  $message['email']='&nbsp;wrong email';

and my foreach json array for the errors :

  foreach($message as $key => $val) {


          $return = array('error' => $key, 'message' => $val );
         echo json_encode($return);

           }

so somthing is wrong here with the foreach for the errors its fire wrong as the ajax response code any idea?

You should not have to split strings in your data. If it is in the correct JSON format, you can access it easily using object notation (also possible via array notation):

var json = '{
  "someVar":true,
  "error":"my fatal error message"
}';

var obj = JSON.parse(json);

alert(obj.error);

This is why JSON was created, for ease of access and use. If you're still going to edit and split strings manually to read data from your JSON, you're doing it wrong.

EDIT: Use nested json, if you need to identify and organise your messages.

var json = "errors": [{
        "id": "001",
        "field": "textfield1",
        "message": "message1."
    },
    {
        "id": "002",
        "field": "textfield2",
        "message": "message2."
    }];

    var obj = JSON.parse(json);

You can now apply styles to these fields, and display the messages:

foreach(obj.errors as err) {
  $("#"+err.field).html(err.message);
  $("#"+err.field).css({"border":"1px solid red"});
}
success:function(data)
        {
          // consider your data is like this JSON string
          // var data= [{"error":"uname","message":"&nbsp;wrong pass."},{"error":"email","message":"&nbsp;wrong name"}];
          // instead of this
          // var data_array = split('*');
          // use this

          //change your json parser to this
          var data_array = JSON.parse(data);
          for (var i in data_array )
          {
             $("#"+data_array[i].error+"_error").html(data_array[i].message);
             $("#"+data_array[i].error).css({"border":"1px solid red"});
          }    
}

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