在另一台服务器上调用php文件并使用javascript显示resullt

时间:2018-08-28 07:17:34

标签: javascript php ajax cors

编辑

我将php文件的标头设置如下:

header("Access-Control-Allow-Origin: *");

这就是我现在在浏览器中得到的错误:

“在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段Access-Control-Allow-Origin。”

那是我的php文件:

<?php
header("Access-Control-Allow-Origin: *");
require 'db_handler.php';


$criterion_name = $_GET['name'];

$query = $fm->newFindCommand("DOKUMENTY_PHP");
$query->addFindCriterion('Nazwa', "=*" . $criterion_name . "*");
$query->addFindCriterion('Format_dokumentu', "=" . "PDF");
$query->addFindCriterion('web', "=" . "1");
$result = $query->execute();

if(FileMaker::isError($result)){
    echo($result->getMessage());
    return;
}
$i = 0;
// Get array of found records
$records = $result->getRecords();
foreach ($records as $record) {
    echo '<li> <a href="./plik.php/?filename=' . $record->getField('Nazwa') 
. '.pdf">' . $record->getField('Nazwa') . '</a></li>';
    $i++;
}
echo $i . " Pozycje";

?>

我试图将表单从我的网站发送到远程服务器上的php文件,将结果发送回并显示,而不重新加载页面。只要网站和php文件都在同一台机器上,一切都可以正常工作,当分开时它将停止工作。 我认为问题可能出在我如何调用php文件,但我不知道该如何做。

file_get_contents()函数可以正常工作。但是我不认为我可以在这里使用它。

这是我的html代码:

<form id="my_form" align = "center">
Nazwa pliku: <input name="name" id="firstname" type="text" />
<input id="submit_form" type="button" value="Submit">
</form>

<div  id="update_div" onKeyPress="return checkSubmit(event)"></div>

<script>
var submit_button = $('#submit_form');

submit_button.click(function() {
var var_name = $('#firstname').val();
var update_div = $('#update_div');
$.ajax({
    type: 'GET',
    headers: { "cache-control": "no-cache" },
    url: 'http://xxx.xxx.xxx.xxx/show_documents.php',
    data: {name: var_name}, 
    success: function(response){
       update_div.html(response);
       }
    });
});

1 个答案:

答案 0 :(得分:-1)

请在php.ini中启用

 allow_url_fopen=On

使用api或ajax从另一台服务器访问。