javascript - Codeigniter 中带或不带斜线的 Ajax 数据调用出现错误

标签 javascript ajax codeigniter

假设我的网址是 example.com/controller/method/

当我使用这个ajax代码时,它会生成像example.com/controller/method/method这样的URL,但无法获取数据。

    function getProductList() {
    var category = document.getElementById('Category').value;

    $.ajax({
        type: 'POST',
        url: 'GetProductList',
        data: {CategoryId: category},
        dataType: 'json',
        cache:false,
        success: function (response) {

        }
    });
}

但是当我的 URL 是 example.com/controller/method 时,ajax 会正确获取数据。但我想从数据库中获取这两种情况的数据。

最佳答案

通常,URL 字符串与其相应的 Controller 类/方法之间存在一对一的关系。因此,您不能使用 example.com/controller/method/method。URI 中的段通常遵循以下模式: example.com/class/function/id/ ,所以你的最后一个方法参数被视为一个 id。因此,在 Controller 中使用默认参数创建方法Ex。 public function mymethod($method = ''){/** 这里是你的逻辑 */}

关于javascript - Codeigniter 中带或不带斜线的 Ajax 数据调用出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254691/

相关文章:

javascript - 使用 JavaScript 的计时器库

javascript - 如果 <td> 具有最大的 int,在其 <tr> 中将 css 类添加到 <td> - jquery

javascript - 使用 jQuery 和 Ajax 访问 JSON 文件

php - CodeIgniter:为什么我的唯一表数据数组不包含任何值?

php - Url 在 codeigniter 中无法正常工作,给出错误 "No input file specified"。

javascript - 在 JS 幻灯片中单击图像时转到其他 URL 和 anchor

javascript - 简单的 Ajax jQuery get 方法未完成

php - 重音字符破坏 PHP 中的 json_encode()

php - Codeigniter 插入代码不起作用

javascript - Error Expected to return a value at the end of 箭头函数