簡體   English   中英

AutoSuggest on Codeigniter(Ajax)-404錯誤

[英]AutoSuggest on Codeigniter (Ajax) - 404 error

我有一個可以正常使用AutoSuggest JS腳本的php應用程序,現在我將同一應用程序移植到codeigniter。 我對CI不太滿意,這就是我想嘗試一下的原因。 問題是它不起作用。 下面是代碼。

JS部分

  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);

控制器上的代碼

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 "]}";
    }

    }

現在,當我直接訪問控制器時,它會正確返回json。

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

{“結果”:[{“ id”:“ 3”,“值”:“ HSBC”,“信息”:“ HSB”}]}}

但是,當我嘗試使用JS時,卻出現404錯誤。 當我跟蹤網絡調用表單檢查元素時,響應是CI的默認404錯誤頁面。

誰能幫我解決這個問題。

檢查您的請求網址。
在大多數情況下,使用localhost時 ,由於URL中的一些錯誤,ajax請求失敗。

例如,您的腳本在localhost/dc但是您的ajax請求發送到localhost/ 您可以在localhost上配置虛擬主機和設置域,或者在所有ajax請求上設置基本URL,這些URL必須是絕對URL。
要檢查請求的去向,可以在開發工具下的firefox / chrome網絡標簽中檢查請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM