CodeDezk CodeDezk - 4 months ago 29
Ajax Question

PHP cannot pass two parameter to another php using ajax

I need to call php and get data from another server and I am using proxy.php to call from ajax.

proxy.php

<?
header('Content-type: application/json');
$url=$_GET['url'];
$json=file_get_contents($url);
echo $json;
?>


And my code looks

function scanFunction(){

var url="http://address/scan.php?user=user1&video=video1";
console.log(url);

url = 'proxy.php?url='+url;

$.ajax({

url: url,
type: "POST",
data: {

},
dataType: "JSON",
success: function (jsonStr) {
if(jsonStr.length>0){
var obj = jsonStr;
console.log(obj);
}
else{
console.log(" error...");
}

}

});

}


And this code works fine when I use one parameter to the url passing to proxy.php where as second argument missing

That is

echo $url;
inside proxy.php print
http://address/scan.php?user=user1


event I pass two argument like,
proxy.php?url="http://address/scan.php?user=user1&video=video1"


That is second argument video missing inside proxy.php and so I am not getting expected result.

Answer

You may be having issues with your GET variables in the GET['url'] variable.

Try encoding your url when you send it to proxy.php to avoid such issues.

var url = encodeURIComponent("http://address/scan.php?user=user1&video=video1");
url = 'proxy.php?url='+url;

Then on the PHP side you need to decode it.

$url=$_GET['url'];
if (is_string($url)) {
    $url = urldecode($url);
}
Comments