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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?