jQuery的:如何阻止AJAX功能逃逸用于发布数据JSON字符串 [英] jQuery: How to stop AJAX function escaping JSON string used to POST data

查看:159
本文介绍了jQuery的:如何阻止AJAX功能逃逸用于发布数据JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要所有的输入从表单序列化为一个JSON字符串。
随着这个帖子,我可以成功地创建一个有效的字符串,如下

  {input01:value01,input02:value02,input03:value03}
 

然而,当我尝试使用字符串使用jQuery的Ajax功能,POST数据,似乎反斜线添加到字符串,导致JSON字符串使用GET而不是POST发送。 加载的PHP页面返回的$ _GET数组:

  [{\input01 \:\value01 \,\input02 \:\value02 \,\input03 \:\value03 \} ] =>
 

我已经测试使用JSON字符串警报()确认结构中的AJAX功能在使用前是正确的。
此外,如果我只是手动输入有效的JSON字符串,AJAX的正确职位的数据。

我的code是如下:

  VAR dataJSON = $ .toJSON($('#形式')serializeObject());
警报(dataJSON);

$阿贾克斯({
    键入:POST,
    网址:ajax.php
    数据:'Query01 = 01&安培; Query02 = 02',
    数据类型:JSON,
    成功:功能(数据){
       如果(数据== 1){
         $('#包装)的负载('ajax.php,dataJSON)。
       }
    }
});
 

解决方案

淘谷歌和jQuery的网站后,我已经到了个人的结论是, $。负载函数将传递给它的任何变量(上述至于我原来的问题)转换为查询字符串。 如果你想通过它来传递一个JSON字符串,它必须手动输入。

要解决这个问题,我使用的低级别 $。阿贾克斯函数。 使用这种方法的优点意味着使用标准 .serialize()的功能,而不是我的表单数据转换成JSON我也可以发送POST数据。

我的最后code:

  VAR FORMDATA = $('#形式')序列化()。

$阿贾克斯({
     键入:POST,
     网址:ajax.php
     数据:'Query01 = 01&安培; Query02 = 02',
     数据类型:JSON,
     成功:功能(数据){
          如果(数据== 1){
               $阿贾克斯({
                    键入:POST,
                    网址:ajax.php
                    数据:FORMDATA,
                    成功:函数(HTML){
                         $(#包装)replaceWith(HTML)。
                    }
               });
          }
     }
});
 

如果任何人有一个解决方案,请评论。

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":"value01","input02":"value02","input03":"value03"}

However, when I try to use the string to POST data using jQuery's Ajax function, it seems to add backslashes to the string, resulting in the JSON string being sent using GET rather than POST. The loaded PHP page returns a $_GET array of:

[{\"input01\":\"value01\",\"input02\":\"value02\",\"input03\":\"value03\"}] =>

I have tested the JSON string using alert() to confirm the structure is correct before being used in the AJAX function.
Additionally, if I just manually type in the valid JSON string, the AJAX posts the data correctly.

My code is as follows:

var dataJSON = $.toJSON($('#form').serializeObject());
alert(dataJSON);

$.ajax({
    type: "POST",
    url: "ajax.php",
    data: 'Query01=01&Query02=02',
    dataType: 'json',
    success: function(data){
       if (data==1){
         $('#wrap').load('ajax.php',dataJSON);
       }
    }
});

解决方案

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.

这篇关于jQuery的:如何阻止AJAX功能逃逸用于发布数据JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆