Javascript ajax请求无法与php通信

Javascript ajax请求无法与php通信,javascript,php,html,ajax,Javascript,Php,Html,Ajax,背景:我有一个包含一些JavaScript的HTML文件。此文件位于服务器上。在同一个目录中,有一个PHP文件。记住这一点 用户选择一些选项,站点根据这些选项生成一个XML字符串。然后,我想将这个XML字符串传递给PHP文件,以生成一个XML文件,并在此服务器上执行一个关于该文件的命令 问题:我在尝试AJAX GET请求时收到错误400(错误请求)。为什么?是因为文件在同一个目录中吗 JS AJAX: $.ajax({ type: "GET", u

背景:我有一个包含一些JavaScript的HTML文件。此文件位于服务器上。在同一个目录中,有一个PHP文件。记住这一点

用户选择一些选项,站点根据这些选项生成一个XML字符串。然后,我想将这个XML字符串传递给PHP文件,以生成一个XML文件,并在此服务器上执行一个关于该文件的命令

问题:我在尝试AJAX GET请求时收到错误400(错误请求)。为什么?是因为文件在同一个目录中吗

JS AJAX:

        $.ajax({
        type: "GET",
        url: 'Submit_Job_to__Computer_Cluster.php',
        data: {XML_requested_job :  XML_string},
        dataType: "json",
        success: function (msg) {
           console.log(msg);
        },
        error: function (errormessage) {
            console.log("error: " + errormessage);
        }
    });
<?php
header("Access-Control-Allow-Origin: *");
$today = getdate();
$year = (string) $today['year'];
$month = (string) $today['month'];
$day = (string) $today['mday'];
$XML_string = $_GET["XML_requested_job"]; //here's where the query data comes into play
$db_path = " /tmp/";
$db_path .= $year;
$db_path .= $month;
$db_path .= $day;
$db_path .= ".db";
$rocoto_path = "/work/apps/gnu_4.8.5/rocoto/1.2.1/bin/rocotorun";   
$XML_file= "workflowPROD.xml";
$file_handle = fopen($XML_file, 'w') or die("Can't open the file");
fwrite($file_handle, $XML_string);
fclose($file_handle);
//concatenate command
$exec_command = $rocoto_path;
$exec_command .= " -w ";
$exec_command .= $XML_file;
$exec_command .= " -d";
$exec_command .= $db_path;
echo json_encode($XML_string);
shell_exec($exec_command);?>
PHP:

        $.ajax({
        type: "GET",
        url: 'Submit_Job_to__Computer_Cluster.php',
        data: {XML_requested_job :  XML_string},
        dataType: "json",
        success: function (msg) {
           console.log(msg);
        },
        error: function (errormessage) {
            console.log("error: " + errormessage);
        }
    });
<?php
header("Access-Control-Allow-Origin: *");
$today = getdate();
$year = (string) $today['year'];
$month = (string) $today['month'];
$day = (string) $today['mday'];
$XML_string = $_GET["XML_requested_job"]; //here's where the query data comes into play
$db_path = " /tmp/";
$db_path .= $year;
$db_path .= $month;
$db_path .= $day;
$db_path .= ".db";
$rocoto_path = "/work/apps/gnu_4.8.5/rocoto/1.2.1/bin/rocotorun";   
$XML_file= "workflowPROD.xml";
$file_handle = fopen($XML_file, 'w') or die("Can't open the file");
fwrite($file_handle, $XML_string);
fclose($file_handle);
//concatenate command
$exec_command = $rocoto_path;
$exec_command .= " -w ";
$exec_command .= $XML_file;
$exec_command .= " -d";
$exec_command .= $db_path;
echo json_encode($XML_string);
shell_exec($exec_command);?>

最可能的原因是你说:

此内容类型会触发,因为它不在安全内容类型列表中(即,您可以使用普通HTML表单触发的内容类型)

您可以使用浏览器开发人员工具的“网络”选项卡检查是否存在这种情况

如果服务器配置不正确,它可能会以400错误请求错误响应选项请求


要解决此问题,请删除该行。由于您没有在请求正文中发布、放置或以其他方式发送HTML文档,因此这是一个谎言。

找到了问题并解决了它

结果是我必须对传递的数据进行编码,新手犯了错误

  • 生成一个变量:
    var encodedXML=encodeURI(XML\u字符串)
  • 改为引用该变量(
    encodedXML
    ):
    data:{XML\u requested\u job:encodedXML}

  • 感谢所有评论和帮助。

    避免在GET请求中发送太多数据。使用POST。为什么客户端内容类型为text/html?您没有发送文本/html。为什么将crossDomain设置为true?
    crossDomain:true,
    -只有当您向同一来源发出请求并将其重定向到另一来源时,才会产生差异。这是一件非常罕见的事情,几乎肯定会膨胀。请手动检查该文件是否存在,并打开浏览器控制台,请添加收到的错误的每一分钟详细信息。如果您只是将其插入URL,您的XML很可能会断开。不要通过聚集字符串将查询字符串组合在一起。传递一个对象并让jQuery正确地转义它<代码>数据:{XML\u requested\u job:XML\u string}
    这是一个跨源请求,
    crossDomain:true
    确保预飞,不是吗?很遗憾,删除这一行并没有解决问题。@etherealite-否。当(a)您发出重定向到不同源的同一源请求时,(b)它停止预飞您没有手动添加会触发飞行前的内容