duanlei2458
2014-08-02 16:40 阅读 40

执行从.post [duplicate]返回的JavaScript

This question already has an answer here:

I want JavaScript to execute the data that is returned from a post. I have found several similar threads but nothing that has cleared it up for me. eval() throws an unexpected identifier error.

The post:

$.post("./save.php", 
  {
    'images_key' : images_array,
    'userid_key' : $('#userid').val(),
    'avatar_key' : $('#avatar_name').val()
  },
  function( data ) 
  {
    $( "#responseText" ).val( data );
    eval(data);
  }
);

The post is returning:

$('.frame').html2canvas({
  onrendered: function (canvas) {
    //Set hidden field's value to image data (base-64 string)
    $('#img_val').val(canvas.toDataURL('image/png'));
    //Submit the form manually
    document.getElementById('myForm').submit();
  }
});

Any ideas?

Thanks!

MORE INFO:

On button click I'm calling this function:

function saveCSS() {

    var id_data = $(".frame div img").map(function() {
      return $(this).attr("id");
    }).get();

    var title_data = $(".frame div img").map(function() {
      return $(this).attr("title");
    }).get();

    var width_data = $(".frame div img").map(function() {
      return $(this).css("width");
    }).get();

    var height_data = $(".frame div img").map(function() {
      return $(this).css("height");
    }).get();

    var top_data = $(".frame div:has(img)").map(function() {
      return $(this).css("top");
    }).get();

    var left_data = $(".frame div:has(img)").map(function() {
      return $(this).css("left");
    }).get();

    var zindex_data = $(".frame div:has(img)").map(function() {
      return $(this).css("z-index");
    }).get();


    var images_array = [];
       $.each(id_data, function (index, value) {
        images_array.push(value);
        images_array.push(title_data[index]);
        images_array.push(width_data[index]);
        images_array.push(height_data[index]);
        images_array.push(top_data[index]);
        images_array.push(left_data[index]);
        images_array.push(zindex_data[index]);
    });

    $.post("./save.php", 
      {
        'images_key' : images_array,
        'userid_key' : $('#userid').val(),
        'avatar_key' : $('#avatar_name').val()
      },
      function( data ) 
      {
        // $( "#responseText" ).val( data );
        // console.log(data);
        eval(data);
      }
    );
}

The file save.php contains:

.
.
.
echo "
$('.frame').html2canvas({
    onrendered: function (canvas) {
        $('#img_val').val(canvas.toDataURL('image/png'));
        document.getElementById('myForm').submit();
    }
});
";
.
.
.

console.log(data) logs the echo as per expected however, in Chrome's console, when I execute saveCSS(), I see:

function( data ) 
      {
        // $( "#responseText" ).val( data );
        // console.log(data);
        eval(data);
**Uncaught SyntaxError: Unexpected Identifier**
      }
    );

I'm afraid I do not understand why I'm getting the error.

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongnanbi4942 dongnanbi4942 2014-08-02 16:45

    In order to execute the dat that is returning you have to eval it e.g.

    $.post("./save.php", 
      {
        'images_key' : images_array,
        'userid_key' : $('#userid').val(),
        'avatar_key' : $('#avatar_name').val()
      },
      function( data ) 
      {
        $( "#responseText" ).val( data );
        eval(data);
      }
    );
    

    That said unless you absolutely trust the code you are receiving from the server, I wouldn't recommend executing the data as it could lead to a XSS vulnerability in your site.

    点赞 评论 复制链接分享

相关推荐