dongzhong8691 2014-11-16 05:35
浏览 39

使用jQuery ajax调用PHP函数

I'm new to PHP and having some trouble calling PHP methods in through jQuery.

I'm using CodeIgniter. The method I want to call is in /application/controllers/home.php . Here's the relevant code:

<?php

class Home extends CI_Controller {

function __construct() {
    // Call the Controller constructor
    parent::__construct();

    ...
}

function addToCart() {
    // code to add to cart
}

The method I'm attempting to call is addToCart.

Here's the jQuery code:

<script>
$(document).ready(function() {
    $(".addToCart").on('click', function () {
        var url = "<?= base_url() ?>home/addToCart";
        console.log('here');
        $.ajax({
            url: url,
            data: {'id': 1},
            type: 'POST'
        });

        return false;
    });
});

</script>

Even though the path is correct, I keep getting a 404.

I can't seem to pin-point what is going wrong. Thank you for your help!

Fixed:

The Home controller was not the default controller for the app. I had to add index.php in front of it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作