0

I have been tweaking this script for a while and as I had mentioned in the question, my code works just fine in the non-asynchronous mode, whereas when I change async:true , the code just fails.

Here is my code.

       $.ajax({
                type : "POST",
                url  : "http://someotherserver.com/sendmail.php",
                data: { resp: data },
                async: false
                })

                    .done(function( msg )
                     {
                        alert(msg); // Works when async:false              
                     })                         

                     .fail(function(jqXHR, textStatus)
                     {
                       if(textStatus == 'timeout')
                       {     
                        alert('Failed from timeout');
                       }
                     });

There is nothing much in the sendmail.php which resides on my friend's server with CORS enabled there.

sendmail.php

<?php
file_put_contents('request.txt',json_encode($_REQUEST));
echo "filecreated";

The problem is when I change the async : true , the request.txt file never gets created on that server whereas if I change async : false it works as expected.

The thing is I want this request to be asynchronous , not synchronous.

Can somebody have any thoughts on this? If anymore information needed, I will be ready to provide.

Catherine Tyler
  • 85
  • 1
  • 10
  • You have to add `Origin` HTTP header if you make CORS request – hindmost Mar 17 '15 at 10:28
  • But it did work in the synchronous mode. CORS is enabled in the other server where the PHP file is present. – Catherine Tyler Mar 17 '15 at 10:29
  • 1
    Perhaps that's affected by the server specifics. However normal [`CORS`](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) request **should** contain `Origin:` header. Otherwise it is not `CORS` request. – hindmost Mar 17 '15 at 10:34
  • @hindmost, Thanks , but this answer says there is nothing to do with JS code. http://stackoverflow.com/a/20442852/1742429 – Catherine Tyler Mar 17 '15 at 10:35
  • @CatherineTyler still weird that it works in sync mode. Any idea why? – AlexStack Mar 17 '15 at 10:41

0 Answers0