douyi6818 2014-06-07 07:57
浏览 52
已采纳

<script>标签未在ajax响应中执行

i am having the same problem as faced by many that the script tags are not executed in the jquery ajax response and i already tried the solutions with the 'eval()' method and similar but none is working.Is there any other way to accomplish this. i am sending a ajax request to a file cart.php from home.php

HOME.php

$.ajax({
    type:"POST",
    url:"cart.php?action=inc&id="+id+"&value="+myvalue+"&linecost="+linecost,
    cache:false,

    success: function(data)
    {
        alert(data);

i am actually converting a php array to a javascript array inside cart.php file like this:

CART.php:

 processing code here...

<?php    
$res=array($val,$cost,$total);
    foreach ($res as $a) {
    $string .= "\"" . $a . "\", ";
    $final = substr($string, 0 ,(strlen($string) - 2));
    }
?>
<script>var j = new Array(<?php echo $final; ?>);</script>

Returned data at home.php inside data variable:

<script>var j = new Array("4", "2928", "6708");</script>

Now i just want the elements of this array but it is returning the whole line at the calling file inside the 'success' function..how do i extract the values at the calling file?Any help?

  • 写回答

1条回答 默认 最新

  • duanshan3427 2014-06-07 08:35
    关注

    You want to use JSON. Instead of returning the script, you can use json_encode.

     echo json_encode($final);
    

    Then to convert the JSON back to an array you can do in JS:

     var result=JSON.parse(data);
    

    result is now a normal JS array containing the same data as in $final.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog