简体   繁体   中英

jQuery AJAX php 'post' returning actual PHP code

I'm unsure how I broke this; it was working previously but now is not. From within an HTML file, I'm making a POST call to a php file (which then grabs data from a REST API), but instead of returning any API data, what I get returned is the actual PHP code.

jQuery AJAX in HTML page:

$.ajax({
  type: "post",
  url: "api.php",
  data: {code: event.data.node.id}
})

api.php:

<?php

$username='[my_username]';
$password='[my_password]';
$code = $_POST['code'];
$URL='https://services.onetcenter.org/ws/online/occupations/' . $code . '/details/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

?>

What I get returned appears to be a string of the PHP code above, rather than the result of the API call. What's going on here? If you need any more information, I'd be happy to help you help me!

EDIT: The second code excerpt was mislabeled as in an HTML file. In fact, the php is within api.php. Sorry!

"jQuery AJAX in HTML page:"

<?php

$username='[my_username]';
$password='[my_password]';
$code = $_POST['code'];
$URL='https://services.onetcenter.org/ws/online/occupations/' . $code . '/details/';
.
.
.

It does that because you have your php code inside a HTML file. Simply change the name of the file to something like "file. php "

As @Itachi said: ".... or if apache, add this in .htaccess AddType application/x-httpd-php .html .htm "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM