title: Access-Control-Allow-Origin error sending a jQuery Post
body: <p>I read lot of the forums for "Access-Control-Allow-Origin" issue. Most of the forum asks to use dataType:'jsonp', but typically jsonp will hit GET request, i want to do POST request. Eventually GET request also not works.</p>
  
  <p>Actually i converting iPhone app to PhoneGap, so re-writing objective-c code to HTML-5 &amp; Jquery mobile. The url which i am trying to hit works very well in objective-c.</p>
  
  <p>Here is objective-c code</p>
  
  <pre><code>NSString *username=[self urlEncodeValue:[userNameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
  NSString *password=[self urlEncodeValue:[passWordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
  NSString *params = [[NSString alloc] initWithFormat:@"username=%@&amp;password=%@",username,password];
  
  NSData *paramData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  [params release];
  NSString *paramsLength = [NSString stringWithFormat:@"%d", [paramData length]];
  
  NSString *urlString=[[NSString alloc] initWithFormat:@"%@?",[dict valueForKey:@"LoginAuthentication"]];
  
  NSURL *authURL = [NSURL URLWithString:urlString];
  [urlString release]; 
  
  NSHTTPURLResponse *authenticationResponse= nil; 
  NSMutableURLRequest *authenticationRequest = [NSMutableURLRequest requestWithURL:authURL];
  [authenticationRequest setHTTPMethod:@"POST"];
  [authenticationRequest setValue:paramsLength forHTTPHeaderField:@"Content-Length"];
  [authenticationRequest setHTTPBody:paramData];
  NSError *error = nil;
  int errorCode;
  
  NSData *responseData = [NSURLConnection sendSynchronousRequest:authenticationRequest  
            returningResponse:&amp;authenticationResponse 
               error:&amp;error]; 
  
  if ([authenticationResponse respondsToSelector:@selector(allHeaderFields)]) {
      //success
  }
  </code></pre>
  
  <p>The above code works very well.</p>
  
  <p>Here's the code which i converted to javascript which gives "Access-Control-Allow-Origin" error</p>
  
  <pre><code>$(document).ready( function() {
      $.ajax({
          type: "POST",
          crossDomain: true,
          //dataType:'jsonp',
          //callback: '?',
          url : "https://externalserver.com/loginAuth?", 
              data : {'username' : 'username', 'password' : 'password'},
          success : function (response) {
              alert('login failed');
          }, 
          error: function(e){
              console.log(e);              
          }
      });
  });
  </code></pre>
  
  <p>I tried with GET/POST request, and tried to run this local file in some local server. Nothing works.</p>
  
  <p>I am getting below error message</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="https://externalserver.com/loginAuth" rel="nofollow">https://externalserver.com/loginAuth</a>?.
    Origin null is not allowed by Access-Control-Allow-Origin.</p>
    
    <p>XMLHttpRequest cannot load <a href="https://externalserver.com/loginAuth" rel="nofollow">https://externalserver.com/loginAuth</a>?.
    Origin null is not allowed by Access-Control-Allow-Origin.</p>
  </blockquote>