-1

I am trying to post a form through AJAX jQuery. The PHP script to which it points returns a JSON encoded array. But, at the receiving end on the main page JSON.parse() is not working.

Please suggest if I am missing on some file types which need to be included

Here is my code.

< script type = "text/javascript" >
  $(document).ready(function() {
    $("#send").submit(function() {
      //$("#submit_form").html('');
      $("#modal-text2").html("<img src=" + "img/loader1.gif " 
              + "/></br</br><h4>DATA VALIDATION IN PROCESS !!! PLEASE WAIT</h4>");
      $("#myModal2").modal('show');

      $.post($("#send").attr("action"), $("#send").serialize(), function(data) {
        var decode = JSON.parse(data);
        if (decode.err > 0) {
          alert("Hi");
        }
      });

      //Important. Stop the normal POST
      return false;
    });
  }); 
< /script>

The JSON encoded array which is being sent back by the PHP script is:

{"err":8,"er1":1,"er3":1,"er4":1,"er5":1,"er6":1,"er7":1,"er8":1,"er9":1,"error1":"First Name is Required","error3":"Last Name is Required","error4":"Email is Required","error5":"Please Select a Gender","error6":"Date of Birth is Required","error7":"Mobile No is Required","error8":"Password is Required","error9":"Please Fill The Captcha"}
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 2
    What do you get when you do `typeof data` – adeneo Aug 09 '16 at 02:51
  • *"is not working"* - Do you get an error in the console? Is jQuery automatically parsing it for you and passing the result in the `data` parameter? – nnnnnn Aug 09 '16 at 03:08
  • typeof data is "string" – Vivek Tariyal Aug 09 '16 at 16:13
  • Besides just on running the HTML code the console displays a warning which says "The specified value "!" does not conform to the required format. The format is "#rrggbb" where rr, gg, bb are two-digit hexadecimal numbers." in jquery.js line 4 – Vivek Tariyal Aug 09 '16 at 16:17
  • basic debugging? `console.log(data, decode)` and see what your received and what it decoded into. – Marc B Aug 09 '16 at 17:23
  • I think the problem lies with jquery.js file of the theme that I am using.........coz the same code runs well with other themes What should i do to check the errors with jquery,js file – Vivek Tariyal Aug 10 '16 at 02:45
  • It is showing unexpected token it means there is a syntax error, in your code snippet there is an space at the closing tag of script < /script>, there is no error in the parse function at all. – Afshan Shujat Aug 10 '16 at 03:41
  • If data *is* a string and the string *is* valid JSON and it *is* a conforming JSON.parse implementation, then `JSON.parse(data)` "will work". If it "doesn't work" then data does *not* evaluate to a valid JSON string. Go back and validate the assumptions to find out which one(s) are incorrect. – user2864740 Aug 10 '16 at 04:24
  • (Also make sure to post *actual* code: `< /script>` will fail to terminate the script element as there may not be a space between the `<` and the `/` in a closing tag.) – user2864740 Aug 10 '16 at 04:26
  • Is there any other method to perform the same operation......... I do not want my page to be refreshed while sending the form data – Vivek Tariyal Aug 10 '16 at 14:35

3 Answers3

0

don't know if its the cause of hte problem or if its just a typo in here, but you have a typo in the following line:

<img src="+"img/loader1.gif "+"/></br</br>

you aren't closing the first linebreak, and the slash should come after the br - also not sure why you have so many quuotes in that html block - it should be :

$("#modal-text2").html("<img src='img/loader1.gif'/><br/><br/><h4>DATA VALIDATION IN PROCESS !!! PLEASE WAIT</h4>")
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0
  • You should console.log(data) to check if the data value has any problem.
  • use try/catch to catch message if error happened in JSON.parse.

    try {
      var decode = JSON.parse(data);
    }catch(e){
      console.log(e) ; 
    }
    
  • Make sure your php responses the json in the right way. Or there may have some invisible character and make the problem.

    <?php 
      $data = ... ;
      header('Content-type:application/json;charset=utf-8');
      echo json_encode($data) ;
    ?>
    
Radian Jheng
  • 667
  • 9
  • 20
  • It Says SyntaxError: Unexpected token in JSON at position 340(…) But the Json String is correct as I have verified it............ It is {"err":8,"er1":1,"er3":1,"er4":1,"er5":1,"er6":1,"er7":1,"er8":1,"er9":1,"error1":"First Name is Required","error3":"Last Name is Required","error4":"Email is Required","error5":"Please Select a Gender","error6":"Date of Birth is Required","error7":"Mobile No is Required","error8":"Password is Required","error9":"Please Fill The Captcha"} – Vivek Tariyal Aug 09 '16 at 16:09
  • Please give me complete error message. The unexpected token and position may be a great hint. – Radian Jheng Aug 09 '16 at 17:03
  • By the way, Make sure your php responses the json in the [right way](http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script). Especially `header('Content-type:application/json;charset=utf-8');`. – Radian Jheng Aug 09 '16 at 17:08
0

I thought there is a sytax error in your script just check it out in the last line of script the closing tag of < /script> has space, remove it and try -

</script>

i execute the parsing snippet of your code it is working fine.

var data = '{"err":8,"er1":1,"er3":1,"er4":1,"er5":1,"er6":1,"er7":1,"er8":1,"er9":1,"error1":"First Name is Required","error3":"Last Name is Required","error4":"Email is Required","error5":"Please Select a Gender","error6":"Date of Birth is Required","error7":"Mobile No is Required","error8":"Password is Required","error9":"Please Fill The Captcha"}';

    var decode = JSON.parse(data);


     if (decode.err > 0) {
        alert("Hi");
      }
Afshan Shujat
  • 541
  • 4
  • 9