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 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。