5
I’ve been reading about this json method and I just don’t understand and don’t know what to fix, I’m using another domain to get the json.
Here is my code:
$(function(){
var url = "http://website.com/json.php";
$.getJSON(url, function(result) {
console.log(result);
$.each(result, function(i, field) {
var id = field.id;
var title = field.title;
$(".class").append("<a href='page.html?id=" + id + "&title=" + title +");
});
});
});
Error:
Xmlhttprequest cannot load http://website.com/json.php. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.
How can I pass this to another code, like this:
$.ajax({
url: url,
crossDomain: true,
data: form,
dataType: 'json',
success: function(data) {
console.log(data);
},
type: 'POST'
});
Json.php:
<?php
header("Access-Control-Allow-Origin: *");
include "db.php";
$data=array();
$q=mysqli_query($con,"select * from `course_details`");
while ($row=mysqli_fetch_object($q)){
$data[]=$row;
}
echo json_encode($data);
?>
Db.php
<?php
header("Access-Control-Allow-Origin: *");
$con = mysqli_connect("localhost","user","pass","db") or die ("could not connect database");
?>
I am opening index.html by Chrome itself, not by localhost, and I am calling on another domain (website.com/json.php), and you have this problem.
A staff put as duplicate. , but it is not duplicated, I would like a solution that in the other has no, accurate has a clear answer.
Anderson, you don’t have the append along.
– user81560
You have tried adding the header('Access-Control-Allow-Origin:*'); at http://website.com/json.php ?
– Julio Soares
Already tried yes Julio, I’ve tried to put extension Chrome too, did not work.
– user81560
I’ve been through everything on the site, I can’t find anything to fix it :/
– user81560