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.

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

报告相同问题?