weixin_33701294 2013-05-08 04:10 采纳率: 0%
浏览 33

为什么PHP脚本无响应?

我试图将变量传递给PHP脚本,用它做一些事情,然后将其传递回我的Javascipt代码。 在这里我将其传递给PHP:

   var src=encodeURIComponent("http://www.someonlinesite.com/file.swf");
   $.ajax({
       url:'test.php?src='+src,
       dataType:'json',
       success:function(response){
           alert(response)
       }
   });

这是脚本:

<?php
   $src=isset($_GET['src'])?$_GET['src']:'';
   $size=getimagesize($src);
   echo json_encode(array('size'=>$size));
?>

我正在尝试将.SWF视频文件的URL传递到一个小的PHP脚本,该脚本将使用 getImagesize()来确定尺寸并将其传回。但是我没有在响应中看到任何内容,并且 alert 也没有触发——出了什么问题?

更新:

根据一些成员的建议,我已经用最新的代码进行了更新。当我对$ src变量进行硬编码并直接导航到test.php时,它会完美地呼应所有内容。因此,看起来PHP正在运行。但是,似乎是回调从未触发或PHP文件未返回数据,在控制台中,响应中仍然没有任何内容。

  • 写回答

2条回答 默认 最新

  • weixin_33713350 2013-05-08 04:13
    关注

    You need to concatenate your url string parameter in get():

    $.get('test.php?src=' + src, function(data){
       alert(data);
    });
    

    And also, your src variable begins with a double quote and is closed with a single quote. That will cause issues.

    var src="http://www.someonelinesite.com/file.swf";
    

    Also, it's probably a bad idea to do this via $_GET since you are passing a URL. $_POST would be better or encode the URL before you pass it. The current url you are passing right now would look like this in a browser:

    http://www.mysite.com/test.php?src=http://www.someonelinesite.com/file.swf
    

    That's not pretty. Using encodeURIComponent(), your whole URL will end up looking like this:

    http://www.mysite.com/test.php?src=http%3A%2F%2Fwww.someonelinesite.com%2Ffile.swf 
    

    Edit to $.ajax

    $.get above would work just fine, but going with the implementation of $.ajax works too:

    $.ajax({
       url:'test.php',
       type: 'GET', //Add the type
       dataType:'json',
       data: {'src': src}, //Add the data, leave it out the url
       success:function(data){
           alert(data)
       }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突