Note that there are some explanatory texts on larger screens.

plurals
  1. PORemote calls via javascript
    primarykey
    data
    text
    <p>I run a service where there is a javascript file that is called and self executed on a user's site.</p> <p>This then calls an external server every 10 or so seconds with a bunch of variables. I used to do this by using a createElement('script') and then setting the path to a file on the external server and passing the required variables across by means of GET variables. (works well for small URI's)</p> <p>This worked really well and seemed to work cross browser as well with no undesired effects.</p> <p>The problem I then ran into was when I needed to extend the <code>amount</code> or <code>size</code> of the variables that were being sent across. So obviously I decided to change from GET method to POST, but by doing that I could no longer use the createElement('script') trick and had to opt for the XMLHttpRequest() (ala Ajax - without jQuery) method which worked really well, except for the minor problem of having to also cater for Internet Explorer and Opera which didn't really play ball too well (big shock). So I used the following:</p> <pre><code>function createCORSRequest(method, url){ var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr){ xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined"){ xhr = new XDomainRequest(); xhr.open(method, url); } else { xhr = null; } return xhr; } var request = createCORSRequest("post", "http://xx.xxxx.com/"); if (request){ request.onload = function(){ //do something with request.responseText }; request.send(myPostObjectDataVariableGoeshere); } </code></pre> <p>..which I found over at <a href="http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/" rel="nofollow">this page</a></p> <p>This is basically just a fallback to using the XDomainRequest() method which InternetExplorer wants you to use instead..</p> <p>Fantastic, BUT -> Looking in the Console of Developer Tools in IE it says:</p> <pre><code>SEC7118: XMLHttpRequest for http://xx.xxxx.com/ required Cross Origin Resource Sharing (CORS). SEC7120: Origin null not found in Access-Control-Allow-Origin header. SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied. </code></pre> <p>But what's really odd about this is that I've already got the following as the first line in my backend PHP file that is being called (which works for other browsers...)</p> <pre><code>header('Access-Control-Allow-Origin: *'); </code></pre> <p>Someone please tell me what's wrong here.. Also if there is a better way to be doing this instead of fighting the browser wars..</p> <p>Note: I cannot use jQuery for this task!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload