Solved

XMLHttpRequest returns Access-Control-Allow-Origin

Posted on 2014-11-19
4
2,596 Views
Last Modified: 2014-11-19
Hi All,

I have the following code;

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<table class="mGrid" id="jsondata">
<thead>
<th>ID</th>
<th>Customer</th>
<th>Contact</th>
</thead>
<tbody></tbody>
</table>
</div>

<script type="text/javascript">

$(document).ready(function(){
	var url="https://www.mywebsite.com/getjson.php";
	$("#jsondata tbody").html("");
	$.getJSON(url, function(data){
				format: "jsonp"
				$.each(data.users, function(i,user){
					var newRow = "<tr>"+"<td>-</td>"+"<td>"+user._9+"</td>"+"<td>"+user.Con_ConName+"</td>"+"</tr>" ;
					$(newRow).appendTo("#jsondata tbody");
				});
	});
});

</script>

Open in new window


That uses getJSON to grab some data from the server.  This works well on the local server.  However I need to run it via a different URL.  When I do this I get the following error

XMLHttpRequest cannot load https://www.mywebsite.com/getjson.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.


A lot of people on the internet highlight is a cross site security issue.

I have full control over everything, so can make changes to the server (IIS7)


Any suggestions?
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
Greg Alexander earned 500 total points
ID: 40452529
On your remote file you can add a php header:

<?php
header('Access-Control-Allow-Origin: https://www.mywebsite.com');
?>

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 40452564
The connecting page is actually embedded in an HTML5 Blackberry application.

is there a way to tell what the URL it uses?  maybe via a PHP page on the server it could connect to.

Alternatively, is there a way to switch it off/allow everyone
0
 
LVL 2

Author Comment

by:detox1978
ID: 40452650
After a little digging around I found an article that said to enable that setting on IIS I need to add the following to my web.config file;

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

Open in new window



I now see the following error message in the console log

GET https://www.mywebsite.com/getjson.php. 401 (Unauthorized) 

Open in new window


followed by

XHR finished loading: GET "https://www.mywebsite.com/getjson.php". 

Open in new window

0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 40452759
The web.config change I mentioned fixed the issue.  The second 401 was because the web server didnt have anonamous access enabled.

many thanks
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to install and use the NTBackup utility that comes with Windows Server.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question