我试图将变量传递给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文件未返回数据,在控制台中,响应中仍然没有任何内容。
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
$.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)
}
});
Try this :
In Jquery :
var src="http://www.someonelinesite.com/file.swf";
$.ajax({
url:'test.php?src='+src,
dataType:'json',
success:function(response){
alert(response.size);
}
});
In php
$src=isset($_GET['src'])?$_GET['src']:'';
$size=getimagesize($src);
echo json_encode(array('size'=>$size));
?>