title: Converting cURL to JQuery.ajax request
body: <p>Hi I'm working on connecting to an API that is using Layer 7 as an IP authorizer and eGalaxy as a credentials authorizer, when the curl request is sent a line of xml is sent back to me. I'm currently working on localhost, I've implemented the Access-Control-Allow-Origin chrome <a href="https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-US" rel="nofollow noreferrer">extension</a>.</p>
  
  <p>My curl request looks as such:</p>
  
  <pre><code>curl https://client-url/eGalaxy.aspx -H 'Content-Type:text/html' --data '&lt;?xml version:"1.0" encoding="UTF-8"?&gt;&lt;Envelope&gt;&lt;Header&gt;&lt;SourceID&gt;0&lt;/SourceID&gt;&lt;MessageID&gt;131&lt;/MessageID&gt;&lt;MessageType&gt;Authenticate&lt;/MessageType&gt;&lt;/Header&gt;&lt;Body&gt;&lt;Authenticate&gt;&lt;Username&gt;*username*&lt;/Username&gt;&lt;Password&gt;*password*&lt;/Password&gt;&lt;PasswordEncrypted&gt;NO&lt;/PasswordEncrypted&gt;&lt;/Authenticate&gt;&lt;/Body&gt;&lt;/Envelope&gt;' --insecure
  </code></pre>
  
  <p>When I tried to create an ajax request I receive an "Invalid HTTP status code 500" error and "OPTIONS url" which drops down to show:</p>
  
  <pre><code>n.ajaxTransport.k.cors.a.crossDomain.send   @   jquery-2.1.3.js:4
  n.extend.ajax   @   jquery-2.1.3.js:4
  (anonymous function)    @   VM947:2
  InjectedScript._evaluateOn  @   VM899:895
  InjectedScript._evaluateAndWrap @   VM899:828
  InjectedScript.evaluate @   VM899:694
  </code></pre>
  
  <p>My ajax code is as follows:</p>
  
  <pre><code>$.ajax({
    url:'https://client-url/eGalaxy.aspx', 
    data:'&lt;?xml version:"1.0" encoding="UTF-8"?&gt;&lt;Envelope&gt;&lt;Header&gt;
          &lt;SourceID&gt;0&lt;/SourceID&gt;&lt;MessageID&gt;131&lt;/MessageID&gt;
          &lt;MessageType&gt;Authenticate&lt;/MessageType&gt;&lt;/Header&gt;&lt;Body&gt;
          &lt;Authenticate&gt;&lt;Username&gt;*username*&lt;/Username&gt;
          &lt;Password&gt;*password*&lt;/Password&gt;
          &lt;PasswordEncrypted&gt;NO&lt;/PasswordEncrypted&gt;&lt;/Authenticate&gt;&lt;/Body&gt;
          &lt;/Envelope&gt;', 
     type:'POST', 
     contentType:'text/xml', 
     dataType:'xml',
     success: function(data){
     },
     error: function(){
     }
   });
  </code></pre>
  
  <p>Any help with translating into a proper AJAX request would be appreciated!</p>
  
  <p>EDIT: If this makes a difference these are the headers that are returned with the client's xml when the curl is complete(client information deleted)<img src="https://i.stack.imgur.com/W2LDN.png" alt="enter image description here"></p>
  
  <p>This application will be made into a widget as well, so it will not be running off of a hosting site.</p>
  
  <p>UPDATE 1: I'm using @KevinB's suggestion that the CORS headers were still not properly added.</p>
  
  <p>Here is my updated JS code, copied from this <a href="http://www.html5rocks.com/en/tutorials/cors/" rel="nofollow noreferrer">link</a>:</p>
  
  <pre><code>var url = 'https://client-url/eGalaxy.aspx';
  var data = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;Envelope&gt;&lt;Header&gt;&lt;SourceID&gt;1&lt;/SourceID&gt;&lt;MessageID&gt;131&lt;/MessageID&gt;&lt;MessageType&gt;Authenticate&lt;/MessageType&gt;&lt;/Header&gt;&lt;Body&gt;&lt;Authenticate&gt;&lt;Username&gt;*username*&lt;/Username&gt;&lt;Password&gt;*password&lt;/Password&gt;&lt;PasswordEncrypted&gt;NO&lt;/PasswordEncrypted&gt;&lt;/Authenticate&gt;&lt;/Body&gt;&lt;/Envelope&gt;';
  var xhr = createCORSRequest('POST', url);
  xhr.send(data);
  function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
  // Check if the XMLHttpRequest object has a "withCredentials" property.
  // "withCredentials" only exists on XMLHTTPRequest2 objects.
  xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
  // Otherwise, check if XDomainRequest.
  // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
  xhr = new XDomainRequest();
  xhr.open(method, url);
  } else {
  // Otherwise, CORS is not supported by the browser.
  xhr = null;
  }
  return xhr;
  }
  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
  throw new Error('CORS not supported');
  }
  </code></pre>
  
  <p>When run with the CORS Chrome extension off I receive an Access-Control-Allow-Origin =! 'null' error. Knowing that CORS needs Access-Control-Allow-Origin header to =! 'null' will this cause problems in the future with making this into a widget that will be put into a Content Manager system?</p>
  
  <p>With it on the origin is set to 'www.evil.com', with the only error in the code being that it says the xhr.send() is an anonymous method. Using breakpoints I can see the xhr in xhr.send() is set to an empty request: </p>
  
  <pre><code>&gt; XMLHttpRequest {response: "", responseText: ""}
  </code></pre>
  
  <p>Inside the createCORSRequest this line is undefined. I've tested using 'GET' and 'POST' as the method. </p>
  
  <pre><code>xhr.open(method, url, true)
  </code></pre>
  
  <p>EDIT 2:
  Using @Fabiano's approach I've changed the web.config for two versions of what I suspect is my server(?). I'm attaching screenshots of what I've gone through
  <img src="https://i.stack.imgur.com/iGvT3.png" alt="the IIS Manager for my system, didn&#39;t see a specific webpage so I chose the ASP Net pages">
  <img src="https://i.stack.imgur.com/KOM5N.png" alt="changed version 2.0">
  <img src="https://i.stack.imgur.com/3PyPI.png" alt="changed version 4.0">
  No luck, so far. Decided to use xhr.AppendHeader:
  <img src="https://i.stack.imgur.com/CXRbk.png" alt="did not recognize method">
  <img src="https://i.stack.imgur.com/bHtUU.png" alt="did not recognize method">
  I decided to use xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
  <img src="https://i.stack.imgur.com/ukYYI.png" alt="xhr.RequestHeader">
  The Network tab Headers for eGalaxy.aspx
  <img src="https://i.stack.imgur.com/ECAUo.png" alt="eGalaxy.aspx headers"></p>