0

Goal: To send XML using XmlHttpRequest as text/xml via POST to PHP file. PHP file then does magic and saves to an XML on the server. A component on an HTML page will be updated with data from the XML.

Problem: I saw success with this when testing with the php web server, which I initiated in command line with php -S 127.0.0.1:8000 but when tested on a Microsoft-IIS server, I got a "405 Method Not Allowed" response. When tested on an Apache server, I get a "200 OK" response, but server XML file is unchanged.

The Javascript code that sends the XML to PHP is something like this:

var xHttp;
if (window.XMLHttpRequest) {
    xHttp = new XMLHttpRequest();
} else {
    xHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xHttp.onreadystatechange = function() {
    if (xHttp.readyState == 4 && xHttp.status == 200) {
        update();
    }
}
xHttp.open("POST", appLocation, true);
xHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xHttp.setRequestHeader("Content-Type", "text/xml");
xHttp.send(xmlDoc);

In the above code, xmlDoc is the XML I want to send and appLocation is the location on the server where the PHP file is. update() is a function I wrote to update a component on an HTML page with data from the XML file that is saved by PHP. From this, I would know that the XML has successfully changed.

The PHP looks like:

$xml = file_get_contents("php://input");
$xmlDoc = simplexml_load_string($xml);
// Magic ***
$xmlDoc -> asXml("some-file-on-server.xml");

If I can be successful on either one Apache or Microsoft servers, I would be content. Thanks in advance! Hopefully this is detailed enough. I found a number of duplicates but nothing that quite helped me solve my problem.

  • Check this post http://stackoverflow.com/questions/24003345/php-405-not-allowed – Jonathan Anctil May 20 '15 at 20:20
  • why set content-type twice? – dandavis May 20 '15 at 20:22
  • Thanks for your response! I guess I should have been more specific, but the link you shared is for an issue related to an Android app. To clarify, my request is sent from an HTML page on the same server where the PHP is located. – tofumonstertruck May 20 '15 at 20:27
  • 1
    @tofumonstertruck The client is irrelevant. If it is a web browser or an Android app or a web-ready microwave oven it doesn't matter because 405 is returned by the server, not the client. – Mike May 20 '15 at 20:28
  • @dandavis you're right, it should probably have been set once! :) Well, writing it like this let me easily toggle single line comment in the IDE I'm using because I had an issues with the content types at some point. – tofumonstertruck May 20 '15 at 20:30
  • The problem is that for some reason IIS is not allowing the POST method. See: http://stackoverflow.com/questions/6617210/http-405-on-error-on-http-post-iis-asp-net – Mike May 20 '15 at 20:34
  • @Mike I wanted to clarify that the request is not cross-domain...or so I don't believe? The request is made from a Javascript file that is on the same server as the PHP file. Someone please feel free to correct me if I'm wrong that this is or isn't a cross-domain request .___.)a – tofumonstertruck May 20 '15 at 20:37
  • As Mike said, a 405 is a server error. If it was a cross origin error, then you'd get a cross origin error. – Quentin May 20 '15 at 20:38
  • it doesn't matter where the js is located, it matters where the html and server-side assets are located. – dandavis May 20 '15 at 20:39
  • Thanks for all responses. My little web app (if you may call it that) consists of four files: a HTML, a Javascript, a PHP, and a XML. All assets are located at the root of the server. Hope this clarifies. I do suspect that this is a server error as well since I have different responses from the IIS and Apache servers. Are there any config settings I should be paying attention to that could be resulting in this? – tofumonstertruck May 20 '15 at 20:46
  • The link I posted does not mention that the requests are cross-domain and gives several possible ways to fix the 405 response with IIS. If that doesn't help, just google "iis 405 post". That was the first result for me. – Mike May 20 '15 at 20:56

0 Answers0