duanreng3439 2016-12-02 13:22
浏览 425

Jquery Ajax使用表单数据传递JSON数组格式

I have an API integartion where the SMS service wants us to send data in this format with content-type as json.

{
  "from": "91887654681",
  "to": ["918757077777"],
  "body": "Hi this is my message using Mblox SMS REST API" 
}

I have a form with input texts namely from, to and body.

This is how my form submits.

$("#sendSMSForm").submit(function(event){
    event.preventDefault();
    // Serialize the form data.
    var form = $('#sendSMSForm');
    var formData = $(form).serialize();
    //alert(formData);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        url: $(form).attr('action'),
        data: formData
    }).done(function(response) {
        // Do some UI action stuff
        alert(response);
    });
});

I am not sure ...what should be used to pass a similar format.... of which the "to" is an array.

  • 写回答

3条回答 默认 最新

  • douao1858 2016-12-02 13:32
    关注

    Simply make your input fields to array

    <input type="number" name="to[]" value="918757077777"/>
    <input type="number" name="to[]" value="918757077778"/>
    <input type="number" name="to[]" value="918757077779"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型