0

I had this error on my console log:

XMLHttpRequest cannot load http://example.com/jsonconnect1.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I know there are tons of this question had been asked but all of them use 'ajax' called whereby my code is using getjson

$(document).ready(function(){

var url="http://example.com/jsonconnect1.php";

$.getJSON(url,function(json){

// loop through the members here

$.each(json.members,function(i,dat){

$("#msg").append(

'<div class="members">'+

'<h1>'+dat.id+'</h1>'+

'<p>Firstname : <em>'+dat.username+'</em>'+

'<p>SurName : <em>'+dat.mobileno+'</em></p>'+

'<p>Title : <strong>'+dat.total+'</strong></p>'+

'<hr>'+

'</div>'

);

Any thoughts? thanks.

Andrea
  • 11,801
  • 17
  • 65
  • 72
ekky008
  • 17
  • 8

2 Answers2

1

You can have a look at here: This issue has been widely discussed by the community. It is better to understand the concept and then it is pretty easy.

How does Access-Control-Allow-Origin header work?

Origin is not allowed by Access-Control-Allow-Origin

“No 'Access-Control-Allow-Origin' header is present on the requested resource”

What are the security risks of setting Access-Control-Allow-Origin?

Community
  • 1
  • 1
Selay
  • 6,024
  • 2
  • 27
  • 23
0

You are apparently attempting a cross-origin Ajax request. That means you're trying to contact a server on a different domain/port than the one that the originating webpage is on. This is called a cross origin request and is not permitted by default. You can read about a browser's same origin security policy here.

In order to be allowed to make a cross origin request directly, the server that you are making the request to must specifically allow it.

The Access-Control-Allow-Origin header is one way that it would allow this type of access, but it is apparently not applying that header and thus the request is being denied by the browser. You can read about how CORS (cross origin resource sharing) works hereenter link description here.

Other ways to make cross origin requests are using JSONP (also requires cooperation from the destination server to support a JSONP request) or via a proxy (another server that you are allowed to contact that can make the request to the desired server for you and return you the results). The proxy requires your own server code on a server you do have permission to reach, but does not require cooperation from the target server.

Per the doc on this page, it appears that Markit On Demand does support JSONP so you could use that form for the cross origin request. jQuery's ajax supports that format if you set the appropriate dataType: "jsonp" option for $.ajax().

Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46