Get data from php file + ajax

I want to get the data loaded into 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  $.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 the button, I get the value of the text field and call the ajax function. my javascript file is in root / js / file.js file and my php file is in root / loadjson.php file

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 that I always get a warning with “Something went wrong,” but I cannot find a solution. Does anyone see my mistake?

I get this: enter image description here

jsfiddle: http://jsfiddle.net/wKe2U/

+4
source share
5 answers

You do not prevent the submission of your form, you use the form and input type of input. So, while you click on this button, the forms. So, first you stop submitting your form in your ajax code.

Secondly, you use the get method in your ajax code and trying to get the values ​​using POST in your php code. So, kindly use $ _GET or change the ajax code type: "POST"

Thirdly, your URL is invalid, you must use the URL: 'loadjson.php'

here i use the 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  $.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); } } 

I hope you understand where you made a mistake :)

+3
source

I'm not sure, but in js code i see

  type: 'GET', 

but in your php code use the POST method to load the value

 if(isset($_POST['appid']) && !empty($_POST['appid'])) { $appid = $_POST['appid']; } 
+2
source

Try to give

url: 'loadjson.php'

in js file

0
source

You send an ajax GET request and you rely on the $_POST information in the script, just use $_GET .

You get a notification for undefined var $appid , press the red line in your devtools to see the response received, and the corresponding error code.

0
source
 $.ajax({ type: 'GET', url: '../loadjson.php', datatype: 'json' , data: { 'appid': appid }, ... 
0
source

Source: https://habr.com/ru/post/1482140/


All Articles