0

I got this php file which i send a json to another website that i use localy.

<?php

error_reporting(0);

define('HOST','localhost');
define('USER','USERNAME');
define('PASS','PASS');
define('DB','DATABASE');

$con = mysqli_connect(HOST,USER,PASS,DB);
mysqli_set_charset($con, "utf8");

$sql = "SELECT * FROM  `mapsk`";
$res = mysqli_query($con,$sql);
//$questions = mysqli_fetch_assoc($res);
$i=0;
while($row = mysqli_fetch_assoc($res)){
     foreach ($row as $key=>$html){

          $questions['result']['items'][$i]['item']['fields'][$key] = $html;

     }
    $i++;
}

echo json_encode($questions);
mysqli_close($con);

?>

The other website takes with this javascript code the jsonp format

reqwest({
    url: 'http://kulturnett2.delving.org/api/search?query=*%3A*&format=jsonp&rows=100&pt=59.936%2C10.76&d=1&qf=abm_contentProvider_text%3ADigitaltMuseum',
    type: 'jsonp',
    success: function (data) {
        var photos = [];
        data = data.result.items;

        for (var i = 0; i < data.length; i++) {
            var photo = data[i].item.fields;
            if (photo.abm_latLong) {
                var pos = photo.abm_latLong[0].split(',');
                photos.push({
                    lat: pos[0],
                    lng: pos[1],
                    url: photo.delving_thumbnail[0],
                    caption: (photo.delving_description ? photo.delving_description[0] : '') + ' - Kilde: <a href="' + photo.delving_landingPage + '">' + photo.delving_collection + '</a>',
                    thumbnail: photo.delving_thumbnail[0]
                });
            }   
        }

        photoLayer.add(photos).addTo(map);
        map.fitBounds(photoLayer.getBounds());
    }
});

I have tried to call my own json link with this http://tfeditor.com/api/maps.php

But i get error on chrome. XMLHttpRequest cannot load http://tfeditor.com/api/maps.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

What should i change the php file to send jsonp format ??? Thanks a lot for your time reading.

Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • 1
    JSONP is just the JSON response wrapped with a function name. You don't seem to be doing that in your PHP. Please read up on JSONP here http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms – Shreyas Nov 19 '15 at 12:42
  • wrap the json encoded data arounda function like `echo 'callbackFunc(' . json_encode($questions) . ')';` – Ashish Choudhary Nov 19 '15 at 12:43

0 Answers0