douwei1408 2017-02-14 05:11
浏览 31

JQuery AJAX无法执行成功函数

What I'm using: JQuery, mysql_crud.php class

I have a class for donor, inside the class I created a function to add a donor into the database. Any result made by the function will return an array.

donor-class.php

public function addDonor($foo, $bar, $soap, ){
    .
    .
    $message = array('message' => 'Message for user here');
    return $message;
}

The class is then executed by another php file which will return JSON data

add-donor-function.php

$res = $donor->addDonor($foo, $bar, $soap);
header('Content-Type: application/json');
echo json_encode($res);

This PHP file will return JSON data

{
    "message" : "Message for user here"
}

I also have a javascript file to run an ajax function

js_add_donor.js

function addDonor() {
    var name = $('#name').val();
    var ic_no = $('#ic_no').val();
    var gender = $('#gender').find(":selected").val();
    var email = $('#email').val();
    var mobile = $('#mobile').val();
    var b_type = $('#b_type').find(":selected").val();
    var dob = $('#don').val();
    var address = $('#address').val();
    var district = $('#district').find(":selected").val();

    $.ajax({
        url: "/firstblood/php/function/donor-management/add-donor-function.php",
        type: "POST",
        data: {name:name, ic_no:ic_no, gender:gender, email:email, mobile:mobile, b_type:b_type, dob:dob, address:address, district:district},
        dataType: 'json',
        success: function(result, status){
            // Below codes won't run
            alert('heloo');
            alert(status);
            $('#response').text(success.message);
        }, error: function(xhr, status, error) {
            // Below codes run
            alert(xhr.status);
            alert(status);
            alert(error);
        }
    });
    return false;
}

addDonor() is used for a button when clicked.
When the code is executed, the window alerts "200", "parseerror", and "SyntaxError: Unexpected token < in JSON at position 0"

Where am i wrong here ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?