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条回答 默认 最新

  • doukan6564 2014-08-03 08:22
    关注

    check your request URL.
    most of the time when you are working on localhost, ajax request failed because of some mistakes in URL.

    for example your script is under localhost/dc but your ajax request send to localhost/. you can config virtual host and setup domain on your localhost or setting base url on all ajax requests at must be absolute url.
    to check where your requests goes to, you can check them in your firefox/chrome network tab under development tools.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题