JSON AJAX POST后的值发生变化(百分比符号)

时间:2014-08-12 09:03:48

标签: php jquery ajax json

我遇到了问题。我正在使用AJAX构建某种幻灯片编辑器。 我有一个PHP脚本,它有许多功能,可以使用不同的GET参数进行选择。 一切正常。但是当我尝试发布幻灯片以保存它们时,名为transformorigin的值从50%50%0变为50500.

GET slide.php?action = get& id = 1

[{"slideid":1,"position":1,"background":{"id":"3","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174513.jpg"},"delay":4000,"timeshift":0,"active":true,"transition2d":[2,7,9],"transition3d":[],"layers":[{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50% 50% 0","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":-80,"offsety":0,"duration":400,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50% 50% 0","easing":{"id":14,"name":"easeInOutQuint"}}},{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50% 50% 0","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":-80,"offsety":0,"duration":400,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50% 50% 0","easing":{"id":14,"name":"easeInOutQuint"}}}]},{"slideid":2,"position":2,"background":{"id":"4","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174608.jpg"},"delay":4000,"timeshift":0,"active":true,"transition2d":[2,7,9],"transition3d":[],"layers":[]}]

POST slide.php?action = post& id = 1

[{"slideid":1,"position":1,"background":{"id":"3","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174513.jpg"},"delay":4000,"timeshift":0,"active":true,"transition2d":[2,7,9],"transition3d":[],"layers":[{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50500","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":80,"offsety":0,"duration":400,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50500","easing":{"id":14,"name":"easeInOutQuint"}}},{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50500","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":80,"offsety":0,"duration":400,"delay":0,"fade":true,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"50500","easing":{"id":14,"name":"easeInOutQuint"}}}]},{"slideid":2,"position":2,"background":{"id":"4","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174608.jpg"},"delay":4000,"timeshift":0,"active":true,"transition2d":[2,7,9],"transition3d":[],"layers":[]}]

我不明白为什么在post之后transformorigin会发生变化。 当我点击帖子请求的firebug post标签时,它仍然是50%50%0。 但服务器方面,当我做

时,我得到50500
echo file_get_contents('php://input');

在开始。

这是我的Javascript / jQuery:

function saveSlides(success,error) {
  $.ajax({
    url: '/slide.php?action=post&id=' + lid,
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(slides),
    contentType: "application/json; charset=UTF-8",
    success: function(data) {
      if ("undefined" == typeof(data.error)) {
        slides = data;
        if (isFunction(success))
          success(data);
      } else {
        if (isFunction(error))
          error(data);
      }
    },error: function() {
      if (isFunction(error))
        error({"error":{"id":100,"message":"Could not retrieve data."}});
    }
  });
}

即使我指定contentType,它也不会更改内容类型。

提前谢谢

更新1:

使用JavaScript对所有内容进行编码后,这就是我发送给php脚本的内容:

[{"slideid":"1","position":"1","background":{"id":"3","name":"http%3A//test.com/uploads/1/img/20140729_174513.jpg"},"delay":"4000","timeshift":"0","active":"true","transition2d":[2,7,9],"transition3d":[],"layers":[{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":"80","offsety":"0","duration":"1000","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":"-80","offsety":"0","duration":"400","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}}},{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":"80","offsety":"0","duration":"1000","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":"-80","offsety":"0","duration":"400","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}}}]},{"slideid":"2","position":"2","background":{"id":"4","name":"http%3A//test.com/uploads/1/img/20140729_174608.jpg"},"delay":"4000","timeshift":"0","active":"true","transition2d":[2,7,9],"transition3d":[],"layers":[]}]

这是我从php脚本中回来的:

[{"slideid":1,"position":1,"background":{"id":"3","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174513.jpg"},"delay":4000,"timeshift":0,"active":false,"transition2d":[2,7,9],"transition3d":[],"layers":[{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":false,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"5025205025200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":80,"offsety":0,"duration":400,"delay":0,"fade":false,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"5025205025200","easing":{"id":14,"name":"easeInOutQuint"}}},{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":80,"offsety":0,"duration":1000,"delay":0,"fade":false,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"5025205025200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":80,"offsety":0,"duration":400,"delay":0,"fade":false,"rotate":0,"rotatex":0,"rotatey":0,"skewx":0,"skewy":0,"scalex":1,"scaley":1,"transformorigin":"5025205025200","easing":{"id":14,"name":"easeInOutQuint"}}}]},{"slideid":2,"position":2,"background":{"id":"4","name":"http:\/\/test.com\/uploads\/1\/img\/20140729_174608.jpg"},"delay":4000,"timeshift":0,"active":false,"transition2d":[2,7,9],"transition3d":[],"layers":[]}]

所以现在它从50%25%2050%25%200变为5025205025200。

在我的php脚本中添加了urldecode后,它再次返回50500。

更新2:

现在我正在尝试测试php文件。

当我这样做时:

echo file_get_contents('php://input');
die;

我收到了我发送的信息:

[{"slideid":"1","position":"1","background":{"id":"3","name":"http%3A//test.com/uploads/1/img/20140729_174513.jpg"},"delay":"4000","timeshift":"0","active":"true","transition2d":[2,7,9],"transition3d":[],"layers":[{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":"80","offsety":"0","duration":"1000","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":"-80","offsety":"0","duration":"400","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}}},{"background":[],"tag":"h1","content":"Test","style":{"top":"0px","left":"0px"},"transitionin":{"offsetx":"80","offsety":"0","duration":"1000","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}},"transitionout":{"offsetx":"-80","offsety":"0","duration":"400","delay":"0","fade":"true","rotate":"0","rotatex":"0","rotatey":"0","skewx":"0","skewy":"0","scalex":"1","scaley":"1","transformorigin":"50%25%2050%25%200","easing":{"id":14,"name":"easeInOutQuint"}}}]},{"slideid":"2","position":"2","background":{"id":"4","name":"http%3A//test.com/uploads/1/img/20140729_174608.jpg"},"delay":"4000","timeshift":"0","active":"true","transition2d":[2,7,9],"transition3d":[],"layers":[]}]

当我这样做时:

$slides = json_decode(file_get_contents('php://input'),true);
print_r($slides);
die;

我明白了:

Array
(
[0] => Array
    (
        [slideid] => 1
        [position] => 1
        [background] => Array
            (
                [id] => 3
                [name] => http%3A//test.com/uploads/1/img/20140729_174513.jpg
            )

        [delay] => 4000
        [timeshift] => 0
        [active] => true
        [transition2d] => Array
            (
                [0] => 2
                [1] => 7
                [2] => 9
            )

        [transition3d] => Array
            (
            )

        [layers] => Array
            (
                [0] => Array
                    (
                        [background] => Array
                            (
                            )

                        [tag] => h1
                        [content] => Test
                        [style] => Array
                            (
                                [top] => 0px
                                [left] => 0px
                            )

                        [transitionin] => Array
                            (
                                [offsetx] => 80
                                [offsety] => 0
                                [duration] => 1000
                                [delay] => 0
                                [fade] => true
                                [rotate] => 0
                                [rotatex] => 0
                                [rotatey] => 0
                                [skewx] => 0
                                [skewy] => 0
                                [scalex] => 1
                                [scaley] => 1
                                [transformorigin] => 50%25%2050%25%200
                                [easing] => Array
                                    (
                                        [id] => 14
                                        [name] => easeInOutQuint
                                    )

                            )

                        [transitionout] => Array
                            (
                                [offsetx] => -80
                                [offsety] => 0
                                [duration] => 400
                                [delay] => 0
                                [fade] => true
                                [rotate] => 0
                                [rotatex] => 0
                                [rotatey] => 0
                                [skewx] => 0
                                [skewy] => 0
                                [scalex] => 1
                                [scaley] => 1
                                [transformorigin] => 50%25%2050%25%200
                                [easing] => Array
                                    (
                                        [id] => 14
                                        [name] => easeInOutQuint
                                    )

                            )

                    )

                [1] => Array
                    (
                        [background] => Array
                            (
                            )

                        [tag] => h1
                        [content] => Test
                        [style] => Array
                            (
                                [top] => 0px
                                [left] => 0px
                            )

                        [transitionin] => Array
                            (
                                [offsetx] => 80
                                [offsety] => 0
                                [duration] => 1000
                                [delay] => 0
                                [fade] => true
                                [rotate] => 0
                                [rotatex] => 0
                                [rotatey] => 0
                                [skewx] => 0
                                [skewy] => 0
                                [scalex] => 1
                                [scaley] => 1
                                [transformorigin] => 50%25%2050%25%200
                                [easing] => Array
                                    (
                                        [id] => 14
                                        [name] => easeInOutQuint
                                    )

                            )

                        [transitionout] => Array
                            (
                                [offsetx] => -80
                                [offsety] => 0
                                [duration] => 400
                                [delay] => 0
                                [fade] => true
                                [rotate] => 0
                                [rotatex] => 0
                                [rotatey] => 0
                                [skewx] => 0
                                [skewy] => 0
                                [scalex] => 1
                                [scaley] => 1
                                [transformorigin] => 50%25%2050%25%200
                                [easing] => Array
                                    (
                                        [id] => 14
                                        [name] => easeInOutQuint
                                    )

                            )

                    )

            )

    )

[1] => Array
    (
        [slideid] => 2
        [position] => 2
        [background] => Array
            (
                [id] => 4
                [name] => http%3A//test.com/uploads/1/img/20140729_174608.jpg
            )

        [delay] => 4000
        [timeshift] => 0
        [active] => true
        [transition2d] => Array
            (
                [0] => 2
                [1] => 7
                [2] => 9
            )

        [transition3d] => Array
            (
            )

        [layers] => Array
            (
            )

    )

)

所以现在我必须以某种方式解码它。

更新3:

我必须在javascript中使用 encodeURIComponent(),在php中使用 urldecode()

非常感谢你现在的工作。

1 个答案:

答案 0 :(得分:1)

您的文字被解释为character encoded,因为它以%符号开头。另见this stackoverflow question。你能在处理它之前逃脱它吗?

您可以在对对象进行字符串化后对文本进行编码,然后在到达时对其进行解码。检查较高的投票回答on this question