i want to get data that's loaded in my PHP file in javascript.
This is what I do:
$(#submit).click(function() {
// GET VALUE OF APPID
var appid = $(#appid).val()
// GET JSON FROM PHP SCRIPT
$.ajax({
type: 'GET',
url: '../loadjson.php',
data: {
'appid': appid
},
success: function (data) {
alert('success');
},
error: function(jqXHR,error, errorThrown) {
if(jqXHR.status&&jqXHR.status==400){
alert(jqXHR.responseText);
}else{
alert(Something went wrong);
}
}
});
});
When I click a button I get the value of a textbox and call ajax function.
my javascript file is located in root/js/file.js and my php file in root/loadjson.php
My PHP file:
<?php
if(isset($_POST['appid']) && !empty($_POST['appid'])) {
$appid = $_POST['appid'];
}
$json_url ='http://api.url.com/api/gateway/call/1.4/getApp?appid=' . $appid;
$ch = curl_init($json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str = curl_exec($ch);
curl_close($ch);
$data = json_decode($str);
$array = $data;
$object = $array->app[0];
echo $object;
?>
The problem is I get always an alert box with Something went wrong but I can't find the solution. Does someone see my fault?
I get this:
jsfiddle: http://jsfiddle.net/wKe2U/