jQuery: How to stop AJAX function escaping JSON string used to POST data

前端 未结 5 1342
遇见更好的自我
遇见更好的自我 2020-12-11 02:07

I need to serialize all inputs from a form into a JSON string.
With the help of this post, I can successfully create a valid string as below:

{\"input01\         


        
5条回答
  •  盖世英雄少女心
    2020-12-11 02:37

    After scouring Google and the jQuery site, i've come to the personal conclusion that the $.load function will convert any variable passed to it as a querystring (As my original problem above outlined). If you wish to pass a JSON string through it, it has to be manually typed.

    To get around this, I used the low level $.ajax function instead. An advantage of using this method meant I could also send POST data using the standard .serialize() function rather than having to convert my form data into JSON.

    My final code:

    var formData = $('#form').serialize();
    
    $.ajax({
         type: "POST",
         url: "ajax.php",
         data: 'Query01=01&Query02=02',
         dataType: 'json',
         success: function(data){
              if (data==1){
                   $.ajax({
                        type: "POST",
                        url: "ajax.php",
                        data: formData,
                        success: function(html){
                             $("#wrap").replaceWith(html);
                        }   
                   });
              }
         }
    });
    

    If anyone else has a solution, please comment.

提交回复
热议问题