Jquery ajax-向rest api发送json无效

Jquery ajax-向rest api发送json无效,jquery,json,ajax,api,Jquery,Json,Ajax,Api,我已经找了好几个小时了,但仍然不知道如何修复它 我有这样的json数据 { "salutation" : "tes", "location" : "tes", "reference_code" : "tes", "isCustomer" : 1, "email_to" : "yudi@nawadata.com", "phone" : "089999999", "front_name" : "Adhitya", "name" : "A

我已经找了好几个小时了,但仍然不知道如何修复它

我有这样的json数据

{
    "salutation" : "tes",
    "location" : "tes",
    "reference_code" : "tes",
    "isCustomer" : 1,
    "email_to" : "yudi@nawadata.com",
    "phone" : "089999999",
    "front_name" : "Adhitya",
    "name" : "Adhitya",
    "age" : 13,
    "marital_status" : "tes",
    "dependents" : "tes",
    "income" : "100000",
    "cover" :   ["Legacy","Education","Retirement"],
    "total_allocated" : 10000000,
    "total_tuition" : 300000,
    "total_cost" : 200000,
    "total_gap" : 1211212121,
    "target" : "2323232323",
    "file_name" : "tes",
    "language" : "id",
    "isFromUmbrella" : 1,
    "collegeperiod" : 2017,
    "collegeage" : 14,
    "collegecosttotal" : 2000000,
    "sangatpenting" : "tes",
    "tigaprioritas" : "tes",
    "arr_child" : [{
            "name" : "coba",
            "monthly_saving" : 20000,
            "educost" : 2000,
            "destination_university" : 10000,
            "year_to_university" : 1000,
            "year" : 2017
        }],
    "arr_triangle" : [
        {
            "name" : "invesment",
            "background" : "#c00",
            "size" : 1
        },
        {
            "name" : "retirement",
            "background" : "#c00",
            "size" : 1
        },
        {
            "name" : "education",
            "background" : "#c00",
            "size" : 1
        },
        {
            "name" : "legacy",
            "background" : "#c00",
            "size" : 1
        },
        {
            "name" : "health",
            "background" : "#c00",
            "size" : 1
        },
        {
            "name" : "life",
            "background" : "#c00",
            "size" : 1
        }
        ]
}
但每次我点击按钮,它只是加载,没有任何反馈给我,我做错了什么吗

以下是JS代码:

function sendoi()
{
    var ItemJSON = {"salutation":"tes","location":"tes","reference_code":"tes","isCustomer":1,"email_to":"yudi@nawadata.com","phone":"089999999","front_name":"Adhitya","name":"Adhitya","age":13,"marital_status":"tes","dependents":"tes","income":"100000","cover":["Legacy","Education","Retirement"],"total_allocated":10000000,"total_tuition":300000,"total_cost":200000,"total_gap":1211212121,"target":"2323232323","file_name":"tes","language":"id","isFromUmbrella":1,"collegeperiod":2017,"collegeage":14,"collegecosttotal":2000000,"sangatpenting":"tes","tigaprioritas":"tes","arr_child":[{"name":"coba","monthly_saving":20000,"educost":2000,"destination_university":10000,"year_to_university":1000,"year":2017}],"arr_triangle":[{"name":"invesment","background":"#c00","size":1},{"name":"retirement","background":"#c00","size":1},{"name":"education","background":"#c00","size":1},{"name":"legacy","background":"#c00","size":1},{"name":"health","background":"#c00","size":1},{"name":"life","background":"#c00","size":1}]}
    $.ajax({
        url: 'http://localhost/dbsapi/api/data/postpdfeducation',
        type: 'POST',
        contentType: 'application/json',
        data: ItemJSON, 
        dataType: "json",
        success: function(){
           alert('hello');
        },
        error: function(){
            alert('error');
        }
    });
};
HTML:

<button type="submit" onclick="sendoi()" class="site-font-light mobile-handler">Submit</button>
提交

这是因为您正在单击提交按钮,所以父元素可能正在提交。您应该钩住该表单的submit事件,并对该事件调用
preventDefault()
,如下所示:

$(函数(){
$('#form')。关于('submit',函数(e){
e、 预防默认值();
var item={/*您的对象…*/}
$.ajax({
网址:'http://localhost/dbsapi/api/data/postpdfeducation',
键入:“POST”,
contentType:'应用程序/json',
数据:项目:,
数据类型:“json”,
成功:函数(){
log('hello');
},
错误:函数(){
console.log('error');
}
});
});
});

提交

还要注意,您发送的数据是一个对象,而不是JSON。在jQuery将其发送到请求中之前,它不会转换为JSON。

您使用的jQuery版本是什么?这是因为您正在单击submit按钮,所以推测父元素
正在提交。您应该挂接该表单的
submit
事件,并对该事件调用
preventDefault()
。@31piy我正在使用jquery-1.12.4。js@RoryMcCrossan你有没有一个例子,先生,我是html新手,所以我不知道这是什么意思当然,我为你添加了一个答案代码比解释更好,谢谢先生,我可以用我的代码和它的工作定制你的代码:)