【问题标题】:ajax - send json to rest api not workajax - 将 json 发送到 rest api 不起作用
【发布时间】:2018-01-04 09:15:25
【问题描述】:

我已经找了几个小时,但仍然不知道如何解决它,

我有一个这样的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>

【问题讨论】:

  • 你用的是什么版本的jQuery?
  • 这是因为您正在单击提交按钮,所以推测父元素&lt;form&gt; 正在提交。您应该改为挂钩到该表单的 submit 事件,并在该事件上调用 preventDefault()
  • @31piy 我正在使用 jquery-1.12.4.js
  • @RoryMcCrossan 先生,你有一个例子吗,我是 html 新手,所以我不知道这是什么意思
  • 好的,我给你加了答案

标签: jquery json ajax api


【解决方案1】:

这是因为您正在单击提交按钮,所以可能是父 &lt;form&gt; 元素正在提交。您应该挂钩到该表单的提交事件,并在该事件上调用 preventDefault(),如下所示:

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    var item = { /* your object... */ }
    $.ajax({
      url: 'http://localhost/dbsapi/api/data/postpdfeducation',
      type: 'POST',
      contentType: 'application/json',
      data: item,
      dataType: "json",
      success: function() {
        console.log('hello');
      },
      error: function() {
        console.log('error');
      }
    });
  });
});
<form action="/foo" method="post" id="form">
  <!-- form elements here... -->
  <button type="submit" class="site-font-light mobile-handler">Submit</button>
</form>

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

【讨论】:

  • 代码比解释更好,谢谢先生,我可以用我的代码定制你的代码,它的工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2016-01-12
相关资源
最近更新 更多