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 ='.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: /
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./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/wKe2U/
Share Improve this question edited May 22, 2013 at 11:36 nielsv asked May 22, 2013 at 11:17 nielsvnielsv 6,82035 gold badges117 silver badges219 bronze badges 7-
Try alerting something useful like
alert(thrownError);
. – Rikesh Commented May 22, 2013 at 11:19 - I have done that but it doesn't show anything. Just an empty alert box ... – nielsv Commented May 22, 2013 at 11:22
-
Help me understand what do you need
$array = $data;
for? – Robert Commented May 22, 2013 at 11:23 - @Rikesh Well we know it isn't an 400 error, can you use developer tools in your browser to see the request and response? – We0 Commented May 22, 2013 at 11:24
- Please provide a url or jsFiddle. – user2050393 Commented May 22, 2013 at 11:24
5 Answers
Reset to default 3You are not preventing your form submission, you are using form and input button submit type. So, while you clicking on that button your form being submit. So, first you stop your form submission in your ajax code.
Second thing is that you are using method get in your ajax code and trying to get values by 'POST' in your php code. So, kindly use $_GET or change ajax code type: 'POST'
Third thing is that your url is invalid you should use url:'loadjson.php'
here I am sharing code:
//Ajax code
$(function () {
$("#submit").click(function (e) {
// stop form submission first
e.preventDefault();
// GET VALUE OF APPID
var appid = $("#appid").val()
// GET JSON FROM PHP SCRIPT
$.ajax({
type : 'POST',
url : 'loadjson.php',
data: {'appid':appid},
success : function (d) {
alert(d);
},
error : errorHandler
});
});
});
function errorHandler(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
Hope, you understand where you were wrong :)
I am not sure but in your js code I see
type: 'GET',
but in your php code use POST method to load value
if(isset($_POST['appid']) && !empty($_POST['appid'])) {
$appid = $_POST['appid'];
}
Try to give
url: 'loadjson.php'
in your js file
You are issuing a GET
ajax request, and you are relying on $_POST
information in your script, just use $_GET
.
You are getting a notice for undefined var $appid
, click on the red row in your devtools to see the response you are getting, and associated error code.
$.ajax({
type: 'GET',
url: '../loadjson.php',
datatype: 'json' ,
data: {
'appid': appid
},
...