douhao9203 2014-08-03 07:24
浏览 48
已采纳

Codeigniter上的AutoSuggest(Ajax) - 404错误

I have a functioning php application that uses the AutoSuggest JS script perfectly and now I'm porting the same application to codeigniter. I'm not so good with CI that was the reason I wanted to try this out. The problem it it's not working. below are the code.

JS part

  var options = {
        script:"/getPartnerLogo?",
        varname:"input",
        json:true,
        shownoresults:false,
        maxresults:6,
        callback: function (obj) { document.getElementById('partner1').value = obj.info;
        }
    };
    var as_json = new bsn.AutoSuggest('pt1', options);

Code on the controller

function getPartnerLogo(){ 

    $aUsers = array(
      "HSBC",
      "Spinneys"
    );


    $aInfo = array(
      "HSB",
      "SPN"
    );

    $input = trim($this->input->get('input'));
    $len = strlen($input);
    $limit = 6;
    $aResults = array();
    $count = 0;

    if ($len)
    {
        for ($i=0;$i<count($aUsers);$i++)
        {

            if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
            {
                $count++;
                $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
            }

            if ($limit && $count==$limit)
                break;
        }
    }

    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
    header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header ("Pragma: no-cache"); // HTTP/1.0

        header("Content-Type: application/json");

        echo "{\"results\": [";
        $arr = array();
        for ($i=0;$i<count($aResults);$i++)
        {
            $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"".$aResults[$i]['info']."\"}";
        }
        echo implode(", ", $arr);
        echo "]}";
    }

    }

Now when I access the controller direct it returns the json properly.

http://localhost/cd/getPartnerLogo?input=h

{"results": [{"id": "3", "value": "HSBC", "info": "HSB"}]}

But when I try is from the JS it gives me a 404 error. When I track the network calls form inspect elements the response is the default 404 error page from CI.

Can anyone please help me fix this issue.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 使用DWY100k数据集对UEA进行测试,出现报错:IndexError: index 125000 is out of bounds for axis 0 with size 95500
      • ¥15 前端vue实现根据图片url生成pdf文件
      • ¥15 RfidReader资源Q个
      • ¥20 user-agent是否是唯一的,有没有可能相同
      • ¥15 关于#开会#的问题,如何解决?(语言-c++)
      • ¥15 关于#二十四点问题#的问题
      • ¥15 运行kitex的demon出错(求大家解决)
      • ¥15 开发一个类似 typora 这样的软件需要多少钱?
      • ¥15 clearcase7.0安装包
      • ¥15 断点回归模型月度核密度检验不连续