0

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!

5
  • 3
    Is your server configured and working? Looks to me like it's not parsing the file. Commented Nov 12, 2014 at 16:25
  • You cannot have PHP code in a HTML file.. Rename the file to yourfilename.php instead of yourfilename.html Commented Nov 12, 2014 at 16:26
  • That was a mislabeling of the code. It was in api.php. Corrected text above. Commented Nov 12, 2014 at 17:14
  • @ChenAsraf Oddly this occurred randomly, after no explicit changes to the server configuration. Are there any specific checks I can do to ensure it's working properly? XAMPP seems to show it working correctly. Commented Nov 12, 2014 at 17:48
  • @EricPsy We had this "bug" twice in our system. The first time the problem was because a coder left echo $result; in the wrong place. The second time was because a coder left return false; in the wrong place. And it was just a random place in the code both times. Commented Dec 7, 2014 at 11:14

1 Answer 1

4

"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"

Sign up to request clarification or add additional context in comments.

2 Comments

.... or if apache, add this in .htaccess AddType application/x-httpd-php .html .htm
I mislabeled the second body of code. In fact, it was in api.php, not an HTML file. However, I am running apache for this php. itachi's suggestions still apply?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.