[英]jQuery AJAX php 'post' returning actual PHP code
我不確定我是怎么打破的。 它以前曾經工作過,但現在卻沒有。 從HTML文件中,我正在對php文件進行POST調用(然后從REST API獲取數據),但是我沒有返回任何API數據,而是返回了實際的PHP代碼。
HTML頁面中的jQuery AJAX:
$.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;
?>
我返回的似乎是上面的PHP代碼的字符串,而不是API調用的結果。 這里發生了什么? 如果您需要更多信息,我們很樂意為您提供幫助!
編輯:第二個代碼摘錄被誤貼為HTML文件中的標簽。 實際上,該php位於api.php中。 抱歉!
“ HTML頁面中的jQuery AJAX:”
<?php
$username='[my_username]';
$password='[my_password]';
$code = $_POST['code'];
$URL='https://services.onetcenter.org/ws/online/occupations/' . $code . '/details/';
.
.
.
這樣做是因為您將php代碼包含在HTML文件中。 只需將文件名更改為“ file。php ”
就像@Itachi所說的: “ ....或者如果是Apache,請將其添加到.htaccess AddType application/x-httpd-php .html .htm
”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.