简体   繁体   English

jQuery AJAX php'post'返回实际的PHP代码

[英]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. 从HTML文件中,我正在对php文件进行POST调用(然后从REST API获取数据),但是我没有返回任何API数据,而是返回了实际的PHP代码。

jQuery AJAX in HTML page: HTML页面中的jQuery AJAX:

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

api.php: 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. 我返回的似乎是上面的PHP代码的字符串,而不是API调用的结果。 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. 编辑:第二个代码摘录被误贴为HTML文件中的标签。 In fact, the php is within api.php. 实际上,该php位于api.php中。 Sorry! 抱歉!

"jQuery AJAX in HTML page:" “ HTML页面中的jQuery AJAX:”

<?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. 这样做是因为您将php代码包含在HTML文件中。 Simply change the name of the file to something like "file. php " 只需将文件名更改为file。php

As @Itachi said: ".... or if apache, add this in .htaccess AddType application/x-httpd-php .html .htm " 就像@Itachi所说的: “ ....或者如果是Apache,请将其添加到.htaccess AddType application/x-httpd-php .html .htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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