pehr.ans pehr.ans - 1 year ago 72
HTML Question

ajax request failing to communicate with php

Background: I have an HTML file containing some JavaScript. This file is hosted on a server. In the same directory, there is a PHP file. Keep this in mind.

The user selects some options and the site generates an XML string based on those options. I then want to pass this XML string to the PHP file to generate an XML file and execute a command concerning this file on this server.

Problem: I receive an error 400 (bad request) upon attempting the AJAX GET request. Why? Is it because the files are in the same directory?


type: "GET",
url: 'Submit_Job_to__Computer_Cluster.php',
data: {XML_requested_job : XML_string},
dataType: "json",
success: function (msg) {
error: function (errormessage) {
console.log("error: " + errormessage);


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);
//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);

EDIT: Changing the type to POST throws a 501 not implemented error instead.

Answer Source

Found the issue and solved it.

It turns out I had to encode the data being passed, rookie mistake.

  1. Made a variable: var encodedXML = encodeURI(XML_String);
  2. Referred to that variable (encodedXML) isntead: data: {XML_requested_job : encodedXML}

Thanks for all the comments and help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download