开发者

jsonp in codeigniter

开发者 https://www.devze.com 2023-03-22 23:45 出处:网络
I am trying to get jsonp working with codeigniter, I am generating standard json with proper header ..

I am trying to get jsonp working with codeigniter, I am generating standard json with proper header ..

   $this->output
        ->set_content_type('application/json')
        ->set_output($json_data);
    }

so the proper header is set for json and theres nothing wrong with the json generated using the above method..

Now using jquery , I am testing

<script>
  $(document).ready(function(){
  var url = "https://localhost/upload/latest/json/10&callback=?";
    $.getJSON(url,function(data) {
        alert(data);
    }); 
  });
  </script>

it wont show any alert

whereas the jsonp url of twitter shows alert

<script>
  $(document).ready(function(){
  var url = "https://twitter.com/status/user_timeline/twitter.json?count=10&callback=?";
    $.getJSON(url,function(data开发者_开发知识库) {
        alert(data);
    }); 
  });
  </script>

I have enabled query strings in config.

$config['enable_query_strings'] = TRUE;

please guide me how to design the urls for jsonp in codeigniter..

Thanks ...


I'm pretty sure that you have to return this for JSONP, i.e. a call to the specified callback function

<?php header('content-type: application/json; charset=utf-8');

$json = json_encode($data);

echo isset($_GET['callback'])
    ? "{$_GET['callback']}($json)"
    : $json;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号