开发者

Is jQuery.parseJSON able to process all valid json?

This piece of valid json (it has been generated using php's json_encode):

{"html":"form is NOT valid<f开发者_Python百科orm id=\"articleform\" enctype=\"application\/x-www-form-urlencoded\" method=\"post\" action=\"\"><dl class=\"zend_form\">\n<dt id=\"title-label\">&nbsp;<\/dt>\n<dd id=\"title-element\">\n<input type=\"text\" name=\"title\" id=\"title\" value=\"Artikel K\"><\/dd>\n<dt id=\"articleFormSubmitted-label\">&nbsp;<\/dt>\n<dd id=\"articleFormSubmitted-element\">\n<input type=\"hidden\" name=\"articleFormSubmitted\" value=\"1\" id=\"articleFormSubmitted\"><\/dd>\n<dt id=\"submit-label\">&nbsp;<\/dt><dd id=\"submit-element\">\n<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Bewaar artikel\" onclick=\"this.value='Bezig...';\"><\/dd><\/dl><\/form><script type=\"text\/javascript\">\n\t $(\"#articleform\").submit(function(){\n $.post(\"\/admin\/ajax\/contenttree\/node\/9\/ajaxtarget\/ajaxContainer\", $(\"#articleform\").serialize(), function(html){$(\"#ajaxContainer\").html(html);} );\n\t\t return false;\n\t });\n\n <\/script>","newNodeName":""}

is giving

jQuery.parseJSON(data)

and me a hard time.

With this piece of code:

alert('start');
alert(data);
jQuery.parseJSON(data);
alert('stop');

I get a message start and then the data (jsonstring above) is shown. The message "stop" never appears.

When I use this json:

{"html":"test","newNodeName":""}

The message stop eventually appears.

$.ajax({
      url: "/admin/ajax/contenttree/node/" + (node.id).replace("node_", ""),
      success: function(data){

      //this message appears
      alert("succes");

      //this gives undefined
      alert(data.html);

      var result = $.parseJSON(data);

      //this message never appears
      alert("after parse");

        $("#ajaxContainer").html(result.html);
      }
    });

I've verified that my first big chunk of json is valid. Why isn't it processed by jQuery.parseJSON Are there any special characters that don't go well with json?


What you posted is already JSON, no need to parse it. Example:

var data = {"html":"form is NOT valid<form id=\"articleform\" enctype=\"application\/x-www-form-urlencoded\" method=\"post\" action=\"\"><dl class=\"zend_form\">\n<dt id=\"title-label\">&nbsp;<\/dt>\n<dd id=\"title-element\">\n<input type=\"text\" name=\"title\" id=\"title\" value=\"Artikel K\"><\/dd>\n<dt id=\"articleFormSubmitted-label\">&nbsp;<\/dt>\n<dd id=\"articleFormSubmitted-element\">\n<input type=\"hidden\" name=\"articleFormSubmitted\" value=\"1\" id=\"articleFormSubmitted\"><\/dd>\n<dt id=\"submit-label\">&nbsp;<\/dt><dd id=\"submit-element\">\n<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Bewaar artikel\" onclick=\"this.value='Bezig...';\"><\/dd><\/dl><\/form><script type=\"text\/javascript\">\n\t $(\"#articleform\").submit(function(){\n $.post(\"\/admin\/ajax\/contenttree\/node\/9\/ajaxtarget\/ajaxContainer\", $(\"#articleform\").serialize(), function(html){$(\"#ajaxContainer\").html(html);} );\n\t\t return false;\n\t });\n\n <\/script>","newNodeName":""};

alert(data.html);
alert(data.newNodeName);

Am I missing something other piece?, let me know if I'm not understanding correctly.

Update: Change your ajax method to use the native $.ajax support for this:

$.ajax({
      url: "/admin/ajax/contenttree/node/" + (node.id).replace("node_", ""),
      dataType: 'json',
      success: function(data){
        alert("succes");
        $("#ajaxContainer").html(data.html);
      }
    });


I found the answer for my problem. In my question you'll find that I tried to pass the html inside an array with the key html. In the javascript-code I tried to get the html using

jQuery.parseJSON(data);
alert(data.html);

When I renamed the key htmlstring the parseJSON-function worked fine. There's no need anymore now to escape anything after the php-function json_encode does it job.

I believe now that .html is a reserved word in jQuery, so if you want to use parseJSON don't feed it a json with a key named "html".


I'd recommend starting with your excerpt that works and then slowing adding back text from the parts that don't work until you find out which bit is causing trouble. The JSON syntax is very, very simple, I would be surprised if jQuery had issues with it.

I wondered about your "script" tags and "function" (thinking: "I wonder if those are triggering an exploit protection or something?") but a quick check with jQuery 1.4.1 said that wasn't it.

Edit

The problem lies elsewhere, the following works with 1.4.1 or 1.4.2 (didn't work with 1.3.2, but that's because 1.3.2 doesn't have jQuery.parseJSON — smacks head). Walk through your code with a debugger and inspect your data variable to see what it contains.

Test page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>
(function() {
    $(document).ready(pageInit);

    function pageInit() {
        $('#btnGo').click(go);
    }

    function go() {
        $.ajax({
            url: "test.json",
            success: function(data) {
                alert(data);
                var obj = jQuery.parseJSON(data);
                alert("obj.html = " + obj.html);
            }
        });
    }
})();
</script>
</head>
<body><div>
<input type='button' id='btnGo' value='Go'>
</div></body>
</html>

test.json:

{"html":"form is NOT valid<form id=\"articleform\" enctype=\"application\/x-www-form-urlencoded\" method=\"post\" action=\"\"><dl class=\"zend_form\">\n<dt id=\"title-label\">&nbsp;<\/dt>\n<dd id=\"title-element\">\n<input type=\"text\" name=\"title\" id=\"title\" value=\"Artikel K\"><\/dd>\n<dt id=\"articleFormSubmitted-label\">&nbsp;<\/dt>\n<dd id=\"articleFormSubmitted-element\">\n<input type=\"hidden\" name=\"articleFormSubmitted\" value=\"1\" id=\"articleFormSubmitted\"><\/dd>\n<dt id=\"submit-label\">&nbsp;<\/dt><dd id=\"submit-element\">\n<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Bewaar artikel\" onclick=\"this.value='Bezig...';\"><\/dd><\/dl><\/form><script type=\"text\/javascript\">\n\t $(\"#articleform\").submit(function(){\n $.post(\"\/admin\/ajax\/contenttree\/node\/9\/ajaxtarget\/ajaxContainer\", $(\"#articleform\").serialize(), function(html){$(\"#ajaxContainer\").html(html);} );\n\t\t return false;\n\t });\n\n <\/script>","newNodeName":""}


Try with:

alert('start');
data=data.replace('\','\\');
alert(data);
test= jQuery.parseJSON(data);
alert(test.html);
alert('stop');

It works for me.


I've found the answer to the problem! Or at least the origin of the problem.

The problem is caused by the "<" and ">" characters. As soon as i try to pass along those characters, the parseJSON-function fails.

Is there a way to allow those characters?

I've already tried to add header('Content-Type: text/html; charset=utf-8'); to the code that is generating the ajax-response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜