-2

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?

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:

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

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

pehr.ans
  • 109
  • 1
  • 14
  • 1
    Avoid sending too much data with a GET request. Use POST instead. – Ibrahim Jul 29 '16 at 18:15
  • 1
    Why is your client-side content type text/html? you're not sending text/html. Why is crossDomain set to true? – Kevin B Jul 29 '16 at 18:16
  • 1
    `crossDomain: true,` — That only mades a difference if you are making a request to the **same origin** that will be **redirected** to a different one. That is a very rare thing to do so that is almost certainly bloat. – Quentin Jul 29 '16 at 18:17
  • Check if the file exists manually and also please open browser console and please add every minute detail of the error you receive. – Manikiran Jul 29 '16 at 18:18
  • 1
    Your XML is likely to break if you just slap it into a URL. Don't slap a query string together by massing strings. Pass an object and let jQuery escape it properly. `data: { XML_requested_job: XML_string }` – Quentin Jul 29 '16 at 18:19
  • I have fixed the data field and removed the content type that was text/html. I still receive a 400 bad request error. I also changed the type to POST, but that just gave me a 501 not implemented error. – pehr.ans Jul 29 '16 at 18:36
  • Is this a solution? http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – etherealite Jul 29 '16 at 18:45

2 Answers2

2

The most likely reason is that you said:

contentType: "text/html; charset=utf-8",

This content type triggers a preflight OPTIONS request because it isn't on the list of safe content types (i.e. those which you could trigger with a plain HTML form).

You can check if this is the case using the Network tab of your browser's Developer Tools.

If the server isn't properly configured, it could respond to an OPTIONS request with a 400 Bad Request error.

To fix this, remove that line. Since you aren't POSTing, PUTing or otherwise sending an HTML document in the body of the request, it is a lie anyway.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • This is a cross origin request, `crossDomain: true` ensures preflighting does it not? – etherealite Jul 29 '16 at 18:34
  • Unfortunately removing this line did not fix the problem. – pehr.ans Jul 29 '16 at 18:39
  • @etherealite — No. It **stops** preflighting when (a) you are making a same origin request that redirects to a different origin and (b) you aren't manually adding something that would trigger a preflight – Quentin Jul 29 '16 at 18:41
0

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) instead: data: {XML_requested_job : encodedXML}

Thanks for all the comments and help.

pehr.ans
  • 109
  • 1
  • 14