【问题标题】:Is there a way to use jQuery JSONP in CodeIgniter without turning on query strings?有没有办法在 CodeIgniter 中使用 jQuery JSONP 而不打开查询字符串?
【发布时间】:2012-03-08 08:47:34
【问题描述】:

我的代码如下:

$.getJSON("http://xx.xx.x.x/directory/index.php?c=json&m=get_listing&jsoncallback=?", { action:'listings' }, function(data) {
// code
});

这很好用。但是现在我在使用其他库时遇到了很多麻烦,这些库对启用的查询字符串不友好。如果我关闭查询字符串,并将上面的 URL 更改为:

http://xx.xx.x.x/directory/index.php/json/get_listing/jsoncallback=?

不起作用。关于变通的任何想法?

编辑:

当我关闭查询字符串并使用它时:

http://xx.xx.x.x/directory/index.php/json/get_listing/jsoncallback=?

Safari 的控制台显示以下错误:

GET http://xx.xx.x.x/directory/index.php/json/get_listing/jsoncallback=jQuery17102770626428537071_1329431463691?action=categories&_=1329431463695 400 (Bad Request)

【问题讨论】:

    标签: php json codeigniter query-string jsonp


    【解决方案1】:

    还有人对此感兴趣吗?
    我刚刚在我的网站上做了一个测试,将 Angular、Jsonp 和 CodeIgniter 结合起来,效果很好。 但我担心安全隐患。
    如果有人对此有建议,我将不胜感激

    而不是将参数直接发送到控制器网址,如下所示:

    public function getObjectJson($page=null,$limit=null,$search=null){
    
        if($page==null||$page<1){
            $page=1;
        }
        if($limit==null||$limit<1){
            $limit=10;
        }
    
        $objects=$this->Objects_model->getObjectsJson($page,$limit,$search);
    
        echo $objects;
    
    }
    

    在 url 中将它们作为查询发送,并使用控制器中的相应方法获取它们:

    public function getObjectsJsonp(){
        $page=$this->input->get('page');
        if($page==null||$page<1){
             $page=1;   
        }
        $limit=$this->input->get('limit');
        if($limit==null||$limit<1){
           $limit=10;
        }
    
        $search=$this->input->get('search');
    
        $objects=$this->Objects_model->getObjectsJson($page,$limit,$search);
    
        $var=$this->input->get('json_objectCallback');
    
        echo $var.$objects;
    }
    

    在我的 Angular 工厂中,URL 字符串被设置为查询

    objectsApp.factory('objectsFactory', ['$http',function($http) {
    
    return {
    
        getObjectsP: function(p1,p2,p3) {
            //The callback function is harcoded and the prarmeters comes from the angular controller
            return $http.jsonp("<?=base_url()?>Objects/getObjectsJsonp/?callback=json_objectCallback&page="+p1+"&limit="+p2+"&search="+p3).then(function(result) {
              objects = result.data;
              console.log(objects);
              return objects;
            });
        }
    
    }]);
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      修复它。这有效:

      http://xxx.xx.x.x/directory/index.php/json/get_categories/?jsoncallback=?
      

      【讨论】:

        【解决方案3】:

        如果最后一段是实例 7,则 url 必须组成为:

        http://xx.xx.x.x/directory/index.php/json/get_listing/jsoncallback/7
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-23
        • 1970-01-01
        • 2011-09-05
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多