I have read about this method json and I just do not understand and do not know what to fix, I'm using another domain to get 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 can not load link . In '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'm opening the index.html by the same chrome, not by localhost, and I'm making the call in another domain (website.com/json.php), and have this problem.
Some people put it as a duplicate, but it is not duplicated, I would like a solution that the other does not have, I need a clear answer.