简体   繁体   English

jQuery Ajax Post 到其他服务器上的 php 文件不起作用

[英]jQuery Ajax Post to php file on an other server is not working

I'am programming a tool, in which you can link my JavaScript file to your website, and this JavaScript file does a ajax post to a PHP file on MY server.我正在编写一个工具,您可以在其中将我的 JavaScript 文件链接到您的网站,并且此 JavaScript 文件将 ajax 发布到我服务器上的 PHP 文件。

The crazy Thing is that when I link the JavaScript file to a page on my webserver, it works, but when I link the JavaScript file on another website (which has another server) It doesn't works... Here the code:疯狂的事情是,当我将 JavaScript 文件链接到我的网络服务器上的页面时,它可以工作,但是当我将 JavaScript 文件链接到另一个网站(具有另一台服务器)时,它不起作用......这里的代码:

phpwrite2.php: (The file which should create a file on my server) phpwrite2.php:(应该在我的服务器上创建文件的文件)

<?php

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST');
    header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
    exit();
}

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


$myFile = $_POST['filename'];
$myFile = "users/".$myFile;

chmod("users/",0777);

$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST["name"];
fwrite($fh, $stringData."\n");
fclose($fh);
?>  

And the jQuery which does the ajax post to the phpwrite2.php file:将 ajax 发布到 phpwrite2.php 文件的 jQuery:

function writeToFile() {
    
    $.ajax({
        type: 'POST',
        url: 'http://alexehgm.myhostpoint.ch/phpwrite2.php',
        crossDomain: true,
        data: '{"name":name, "filename" : filename}',
        dataType: 'txt',
        success: function(responseData, textStatus, jqXHR) {
            var value = responseData.someKey;
        },
        error: function (responseData, textStatus, errorThrown) {
            alert('POST failed.');
        }
    });

Thanks for reading my question, I hope you pros know a solution or find the fault.感谢您阅读我的问题,我希望您的专业人士知道解决方案或找到故障。

Basically it comes down to sending the correct CORS headers on OPTIONS request (the preflight request) in addition with a header on the successive requests:基本上它归结为在 OPTIONS 请求(预检请求)上发送正确的 CORS 标头以及连续请求上的标头:

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST');
    header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
    exit();
}

header('Access-Control-Allow-Origin: *');
// Your code here

Explanation:说明:

  • Acess-Control-Allow-Origin : Sets the Domains that are allowed to access this resource. Acess-Control-Allow-Origin :设置允许访问此资源的域。
  • Acess-Control-Allow-Methods : Sets the HTTP methods that are allowed to access the resource. Acess-Control-Allow-Methods :设置允许访问资源的 HTTP 方法。
  • Access-Control-Allow-Headers : These are the additional headers that are allowed. Access-Control-Allow-Headers :这些是Access-Control-Allow-Headers的附加标头。 You can use the requested headers to simply allow all requested.您可以使用请求的标头简单地允许所有请求。 This cannot be * .这不能是*

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

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