0

I am building a web-site that calls php on the backend for some pages. When I view the html page on a computer browser, it returns with a status of 200 and shows content returned from php script. However, when I view the same page on my iphone it returns a status of 0 (and subsequently nothing is returned/displayed). Here is a sample of the code - very simple.

Here is what the html page looks like at the script location:

<script>
 function load() {
   var httpr = new XMLHttpRequest();
   httpr.onreadystatechange = function() {
     if (httpr.readyState == 4 ) { // && httpr.status == 200
       alert(httpr.status);
     }
   }

   httpr.open("GET", "https://www.mywebsite/myphpscript.php", true);
   httpr.send();
 }

 load();
</script>

When I go view the page on my iphone, and am tailing the php logfile on my webserver, I do see messages (debug ones I put in the php code), so I know its firing/executing (as of now, the code simply displays some text that I want to parse). If I simply use my iphone to view the php page itself (go directly to the php file v.s. the web-page that includes the php file), it does respond with what I expect to see. It just doesn't work from the html page that invokes the php script via xmlhttprequest.

Any idea why?

1 Answer 1

0

Ok. I thought this only applied to POST php/cgi, but apparently it has to do with allowing anyone anywhere to have access to the script. I had to add this to the php script:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.