0

I am sending post request through AJAX as below.

I am always getting xmlhttp.readyState = 1 and xmlhttp.status= 0 . xmlhttp.responseText is always empty.

Could you please tell me what could be the problem ?

I expect xmlhttp.readyState==4 && xmlhttp.status==200

<script>
//Ajax to send request..
function sendPayment()
{
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
        xmlhttp.onreadystatechange=function()
          {
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                if (xmlhttp.responseText=='1')
                {
                    alert('success');
                }   
            }
          }
    xmlhttp.open("POST","payments/callSSL.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(Id=100);
    return false;
}
</script>

HTML PART

<input name="button" type="submit" id="button" value="Confirm" onclick="sendPayment()" />
logan
  • 7,946
  • 36
  • 114
  • 185
  • And does the `success` alert never show ? – adeneo Jul 19 '14 at 05:54
  • xmlhttp.readyState always return true having ajax always return a value. please specify your status of ajax result – TBI Jul 19 '14 at 05:55
  • @adeneo : yes. I get only empty though i send some outputs in `/callSSL.php` file – logan Jul 19 '14 at 06:03
  • check your callSSL.php , you have to echo not return your result – rajesh kakawat Jul 19 '14 at 06:19
  • @rajeshkakawat : i just added `echo "1"` in the first line of callSSL.php file. still no use. same result.. – logan Jul 19 '14 at 06:23
  • call your callSSL.php directly from browser address bar see what you get????? – rajesh kakawat Jul 19 '14 at 06:29
  • @rajeshkakawat : callSSL.php is my another site. i am calling from one site to another site. I just updated my question `http://my-other-site.com/payments/callSSL.php` - sorry for the miss – logan Jul 19 '14 at 06:30
  • 1
    you can't call from another domain check this out http://stackoverflow.com/questions/23053349/getting-cross-origin-block-request-cors-error-when-using-getjson-to-get-play – rajesh kakawat Jul 19 '14 at 06:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57594/discussion-between-logan-and-rajesh-kakawat). – logan Jul 19 '14 at 06:44
  • @rajeshkakawat : I just moved my callSSL to same domain but still same issue. i think something else is issue – logan Jul 19 '14 at 06:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57595/discussion-between-rajesh-kakawat-and-logan). – rajesh kakawat Jul 19 '14 at 06:55

2 Answers2

1

If you are calling to your own another site,you have to give permission in your another site ie(http://my-other-site.com/payments/callSSL.php) to be accessed.

Put this header in your http://my-other-site.com/payments/callSSL.php

header('Access-Control-Allow-Origin: *'); 

for specific page

header('Access-Control-Allow-Origin: http://www.yourxmlrequestpage.php');

Hope this helps ,

thank you

SarathSprakash
  • 4,614
  • 2
  • 19
  • 35
-1

you need to add a var as xmlhttp; on starting to get the status result please use below code i modify it,

<script>
//Ajax to send request..
function sendPayment()
{
var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
        xmlhttp.onreadystatechange=function()
          {
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                if (xmlhttp.responseText=='1')
                {
                    alert('success');
                }   
            }
          }
    xmlhttp.open("POST","payments/callSSL.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(Id=100);
    return false;
}
</script>
TBI
  • 2,789
  • 1
  • 17
  • 21
  • same result.. No use.. i have used many ajax request where i have not all specified it as `var` – logan Jul 19 '14 at 06:16
  • Add `xmlHttpRequest.send();` after `xmlHttpRequest.onreadystatechange = processRequest;`. please try this may help you. – TBI Jul 19 '14 at 06:28
  • you please use with 'var xmlhttp;' hope this help or make sure it is processRequest function. – TBI Jul 19 '14 at 06:37