No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access. - Phonegap

Asked

Viewed 35,879 times

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.

  • You have tried adding the header('Access-Control-Allow-Origin:*'); at http://website.com/json.php ?

  • Already tried yes Julio, I’ve tried to put extension Chrome too, did not work.

  • I’ve been through everything on the site, I can’t find anything to fix it :/

2 answers

4


If you don’t have access to the server to add headers:

You can use one of the extensions for Chrome (for development):

Allow-Control-Allow-Origin: *

CORS Toggle

These extensions disable Chrome security with respect to communication from different hosts and without the authorization setting in the header (as done by the header function() ).

In short this error is fixed by adding: (PHP)

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
echo json_encode($data);

Or your API-consuming front end must be on the same host(IP+port) as your API.

  • No header does not work, useu the extension error left, but nothing appeared, in the console appeared this: jquery.js:4 XHR finished loading: GET "website.com/json.php". send @jquery.js:4 ajax @jquery.js:4 n.(Anonymous Function) @jquery.js:4 getJSON @ jquery.js:4 (Anonymous) @ index.html:203 j @ jquery.js:2 fireWith @ jquery.js:2 ready @jquery.js:2 I @jquery.js:2 if I swap and put using the host server and opening the right index.html file works normal, when it is on another server of these errors, I had used a server with https and it worked, but it has expired.

0

Everything indicates that it is a communication problem with the server of your site. This occurs when you try to make a call to a server that is in a domain other than the origin of your request. I don’t know what technology you are using on your site, but it is possible to enable this communication.

Search on how to enable CORS on the technology you are using on the site.

  • How can I enable this communication?

  • You can do this by following this tutorial: https://enable-cors.org/server_php.html

  • I’m using html because it’s phonegap.

  • Try a different way to make the call: $.ajax({&#xA; url: 'http://website.com/json.php',&#xA; dataType: 'json',&#xA; crossDomain: true,&#xA; success: function (data) {&#xA; console.log(data)&#xA; }&#xA;});

  • I’ve tried too, unsuccessfully.

  • Your ajax is being issued from inside an http server or you are running directly from the file?

  • Straight into the file, using json.php from a server.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.