douji6896 2018-08-13 09:59
浏览 173
已采纳

如何使用jquery ajax使用自定义json结构发布数据?

I am using Wordpress with Woocommerce platform. I must send selected order details to an API(3rd party accounting system) after an order has been successfully made.

Current jquery ajax code

  j.ajax({
    type: 'POST',
    url: 'https://mywebsite/api/orderupload',
    cache: false,
    data: {

    },
    beforeSend:function() {

    },
    success: function (result) {
    },
    error: function(xhr,status,error) {
      console.log(error);
    },
    complete:function(){
    }
  });

The structure of the json data that must be inside data {} to the post request above is:

{
  "order": {
    "customer": {
      "type": "public",
      "vat": "false",
      "org_nr": "123456-789",
      "name": "Carl Lorem",
      "phone": "639055543227",
      "address": {
        "name": "Carl Lorem",
        "address": "Riften Street",
        "zipcode": "2323",
        "city": "Dawnstar City",
        "country": "US"
      },
      "email": "carllorem@gmail.com"
    },
    "transaction": {
      "storage": {
        "name": "Los Angeles",
        "location": "Road Earth",
      },
      "insurance": {
        "name": "Igsum Nat",
        "cost": "30"
      },
      "date": {
        "start": "2018-4-23",
        "end": ""
      }
    }
  }
}

Do you know how can I structure my json data for my post request? Any idea is appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • douzhouqin6223 2018-08-13 10:04
    关注
    {
        "order": {
            "customer": {
                "type": "public",
                "vat": "false",
                "org_nr": "123456-789",
                "name": "Carl Lorem",
                "phone": "639055543227",
                "address": {
                    "name": "Carl Lorem",
                    "address": "Riften Street",
                    "zipcode": "2323",
                    "city": "Dawnstar City",
                    "country": "US"
                },
                "email": "carllorem@gmail.com"
            },
            "transaction": {
                "storage": {
                    "name": "Los Angeles",
                    "location": "Los Angeles, Road Street"
                },
                "insurance": {
                    "name": "Igsum Nat",
                    "cost": "30"
                },
                "date": {
                    "start": "2018-4-23",
                    "end": ""
                }
            }
        }
    }
    

    The json you attached was invalid i send you a correct one,

    About the ajax part:

    $.ajax({
           type: "POST",
           url: '/mywebsite/api/orderupload',
           data: ("myjson": myjson},
           dataType: 'json'
     success: function (response) {
    console.log(response)
    }
       error: function(xhr,status,error) {
          console.log(error)
        },
    })
    

    The myjson variable is your json that you want to send through ajax. After that your back-end takes over on how you handle it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)