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.
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);
}
proxy.php cannot know which arguments are meant for it, and which are meant for scan.php. Eg, When you call url:
proxy.php?url=http://address/scan.php?user=user1&video=video1
Your proxy scripts thinks that the query parameters are:
url:"http://address/scan.php?user=user1"
video:"video1"
But your intent was for everything after url to be one parameter. A better approach is to use POST parameters instead of URL query parameters.
$.ajax({
url: url,
type: "POST",
data: {
resource_url: "http://address/scan.php?user=user1&video=video1"
},
....
});
Now, in proxy.php:
<?php
header('Content-type: application/json');
$url=$_POST['resource_url'];
$json=file_get_contents($url);
echo $json;
?>
Use the data: property of the ajax call to pass as much or as many parameters as you like
function scanFunction(){
$.ajax({
url: 'proxy.php',
type: 'POST',
data: {
url: 'http://address/scan.php',
user: 'user1',
video: 'video1'
},
dataType: "JSON",
success: function (jsonStr) {
if(jsonStr.length>0){
var obj = jsonStr;
console.log(obj);
} else {
console.log(" error...");
}
}
});
}
Then build whatever you want from those parameters in the php script
Oh and you used type: 'POST' in your javascript, so you should be using the $_POST array in your PHP script.
proxy.php
<?php
$url = $_POST['url'] . '?user=' . $_POST['user'] . '&video=' . $_POST['video'];
$json=file_get_contents($url);
header('Content-type: application/json');
echo $json;
?>
Related
I am starting with angularjs with ngStorage. I can save and display data successfully.
I display value as before
{{myobj.session}}
I would like to pass whatever stored value into php variable. Shown below is my imaginary logic and I know thats not gonna work. My question is how to assign such value into PHP variable in a correct manner?
<?php
$name = {{myobj.session}}
?>
You can use this angular variable: students.tiffen_type
PHP variable : $value
<?php
$value = "{{ students.tiffen_type }}";
?>
You can do a ajax request like this:
$http({
url: "urltopost.php",
method: "POST",
data: {
data: variable
}
}).success(function(response) {
console.log(response);
});
And on the backend you can get the variable like this
<?php
$request = json_decode( file_get_contents('php://input') );
$variable = $request->data
From there you can do everything you want with that variable, still I'm not sure what are you trying to achieve.
You can't assign directly angularjs value to php variable, use the following method it will help you
$http({
method: "POST",
url: "test.php",
data: {
data: postvariable
}
}).success(function(response) {
console.log(response); //get the echo value from php page
}).error(function(response) {
console.log(response);
});
test.php
<?php
$data = json_decode(file_get_contents("php://input"));
echo $data->data;
?>
send.js
$http({
method: "post",
url: "ajax/request.php",
data: {
angular_var: $scope.angular_var // $scope.angular_var is angular variable e.g. ng-model="angular_var"
}, headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
console.log(data)
});
ajax/request.php
$request_arr = json_decode( file_get_contents('php://input') );
$angular_var = $request_arr->angular_var;
Please can anyone assist I'm trying to get my JSON data displayed on my html5 localhost page,
I'm still new to JSON
I get the following returned but no data is loading on the page.
http://www.hostname/getCheck.php?callback?&callback=jQuery110205560797746881064_1392215061343&_=1392215061344
Please if anyone can assist.
Below is my php script
`mysql_select_db($database_xxx, $xxx);
$rsfet = "SELECT * FROM cs_tracking ";
$fet = mysql_query($rsfet, $xxx) or die(mysql_error());
$json = array();
while($r=mysql_fetch_array($fet)){
$json[] = $r;
}
header('Access-Control-Allow-Origin: *');
echo $callback ='('.json_encode($json).')';`
and my javascript to display the table data
`
$(document).ready(function(){
$.ajax({
url: 'http://xxxxxxxxxxx.com/getCheck.php?callback=?',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonp: true,
success: function(data){
$.each(data,function(i,photo){
var tblRow =""
+""+data.CS_Track_Child+""
+""+data.CS_Track_Date+""
+""+data.Tracking_Status+""
+""+data.CS_Tracking_ID+""
+"" ;
$(tblRow).appendTo("#userdata tbody");
});
},
});
});`
The $callback variable is not magically declared in your script (at least, it shouldn't be); you can access the value via $_GET['callback'] but make sure to sanitize its value:
if (isset($_GET['callback']) && preg_match('/[A-Z]\w*/i', $_GET['callback']) {
header('Content-Type: application/javascript');
header('Access-Control-Allow-Origin: *');
printf('%s(%s);', $_GET['callback'], json_encode($json));
}
You have two GET parameter of callback one is valid but empty and second is invalid.
http://www.hostname/getCheck.php?callback?&callback=jQuery110205560797746881064_1392215061343&_=1392215061344
url: 'http://xxxxxxxxxxx.com/getCheck.php?callback=?',
So remove your parameter and try with this:
url: 'http://xxxxxxxxxxx.com/getCheck.php',
I have this ajax code
$.ajax(
{
type: "POST",
url: "getData.php",
data: ValueToPass,
cache: false,
success: function(html)
{
LastDiv.after(html);
}
});
I am new with this Ajax thing.
This code is to load getData.php file and send variables through type POST.
The variables are in this var ValueToPass = "lastid="+LastId+"&br="+br;.
Other thing this code does is return the getData.php's HTML after loading.
Probably with this. success: function(html)
How can I return this $br variable from getData.php after loading, so I can use it again through the next cycle. Cuz what happens here is that I can put the variable in the getData.php with the Ajax and working with it, but when the file getData.php is loaded, outside this file, the variable is not known(not declared). And I'm losing the counting :S
I want to return the HTML and the variable.
You can return json data in your php file like
$response = array ('br'=> $br, 'html'=> $html);
echo json_encode($response);
Here both html and data are returned.
And this to use it in your ajax callback :
success: function(data)
{
br = data.br;
LastDiv.after(data.html);
}
I'd consider setting a Session variable with the value from the $br variable passed via AJAX. Then when you call getData.php from another file or location, you can use the Session variable since session variables retain their value anywhere in the session.
You can try this to get the data from your `getData.php' :
$.ajax(
{
type: "POST",
url: "getData.php",
data: { ValueToPass: ValueToPass},
cache: false,
success: function(data)
{
LastDiv.html(data);
}
});
and in your getData.php you have to pass ValueToPass
maybe like this:
$ValueToPass = mysqli_real_escape_string($db, $_POST['ValueToPass']);
If I understand your question correctly, and if you want to return the $br variable then include it in a JSON object in the successs callback function. So, something like this (I'm not familiar enough with PHP so my PHP syntax might be incorrect):
// create JSON object
<?php
$result = array('br' => $br, 'html' => 'htmlContent);
echo json_encode($result);
?>
// return JSON object
$.ajax(
{
type: "POST",
url: "getData.php",
data: ValueToPass,
cache: false,
success: function(result)
{
var $br = result.br;
LastDiv.after(result.html);
}
});
I am trying to pass some data to a web-service using JQuery. Here is a simple client:
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "webservices/gammeList.php?lang=fr",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
error: onError
});
});
function onError(result) {
alert("error");
}
function onSuccess(result){
alert(JSON.stringify(result));
}
</script>
And a simple server:
<?php
if (isset($_GET['lang']) && !empty($_GET['lang'])) {
$lang = $_GET['lang'];
} else {
$lang = "en";
}
echo (json_encode($lang));
?>
It is working properly, but I would like to pass the data using the data setting that way:
$(function () {
$.ajax({
type: "POST",
url: "webservices/gammeList.php",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {lang: "fr"},
success: onSuccess,
error: onError
});
});
I always get "en" as a response from the web-service. So here, should I use the same method $_GET['lang'] to access the input data? What am I doing wrong?
EDIT:
I changed $_GET['lang'] in $_POST['lang'] but still, it doesn't work.
You can use server side $_REQUEST which will work for both POST and GET method:
$_REQUEST['lang']
You are using $_GET["Key"] which is incorrect as you are passing data in POST variables.
You can use either $_REQUEST["key"]
$lang = $_REQUEST["lang"];
or $_POST["key"]
$lang = $_POST["lang"];
to retrieve the data sent to the PHP script.
To read a little bit more about these refer links given below.
$_REQUEST
$_POST
So then use server side $_POST, not $_GET:
$_POST['lang']
You should pass array to json_encode function like,
<?php
// Also you are using post method in ajax so use $_POST or $_REQUEST here
if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) {
$lang = $_REQUEST['lang'];
} else {
$lang = "en";
}
echo (json_encode(array($lang)));// here passing array to json_encode
?>
After changing $_GET['lang'] in $_POST['lang'], it was still not working. I deleted the following line:
contentType: "application/json; charset=utf-8"
using then the default value 'application/x-www-form-urlencoded; charset=UTF-8' and it worked.
I'm trying to use jQuery $.ajax() but I'm facing some difficulties.
Here's the textbox field that I want to use for POST:
<input name="url" class="url" type="text" >
Here's the code:
$.ajax({
type: "post",
url: "file.php",
data: $(this).serialize(),
success: function(data) { ...............
Now this is the file.php:
<?php
if( $_REQUEST['url'] )
{
$url = $_REQUEST['url'];
$url = file_get_contents($url);
// I would need now to return something here but not sure how??!!
}
?>
Now, my question is, how to return variables in this PHP code and use them in my code above, I mean in the success part of $.ajax(). Also if I want to perform some additional stuff on the $url variable, how to do it? How to return them? :/
If you want to return some variables/fields, the best way would be to echo a JSON string. Here is a small example:
PHP Code:
<?php
if( $_REQUEST['url'] )
{
$url = $_REQUEST['url'];
$url = file_get_contents($url);
$result['var1'] = 'something';
$result['var2'] = 500;
echo json_encode($result);
}
?>
JS Code:
$.ajax({
type: "post",
url: "file.php",
data: $(this).serialize(),
dataType: 'json', // maybe not needed? I do not know if jQuery autodetects it
success: function(data) {
// here you can use data.var1 and data.var2 to read the fields
}
});
You just print/echo your 'return' value.
file.php
<?php
if( $_REQUEST['url'] )
{
$url = $_REQUEST['url'];
$url = file_get_contents($url);
// I would need now to return something here but not sure how??!!
echo "something";
}
?>
Then in your JS:
$.ajax({
type: "post",
url: "file.php",
data: $(this).serialize(),
success: function(data) {
console.log(data); // "something"
}
});
As a side note. Your script looks like it accepts any URL and fetches it. It is possible to abuse scripts like that. Make sure you are aware that.