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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵