How do I send a complex JSON object from an HTML form using SYNCHRONOUS page POST?

后端 未结 4 2074
清歌不尽
清歌不尽 2021-01-15 11:21

My server is using ServiceStack and would like to receive some data like this:

{
    Customer: {
        Company: \"TheCompany\",
        RegionCode: \"AU_NS         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 11:40

    ServiceStack can POST complex types using the JSV Format, e.g:

    
    
    

    Otherwise you can send complex types using JSON, e.g. with jQuery's $.ajax:

    $.ajax({ 
       type: 'POST',
       contentType: 'application/json; charset=utf-8',
       url: "http://host/myservice",
       dataType: 'json',
       data: JSON.stringify({Customer:{Company:'x',RegionCode:'x'}}),
       success: function(response){ ... }
    });
    

    Although for maximum interoperability you should strive to keep your Request DTO's flat, e.g:

    Which you can then POST as-is which the browser will do using the x-www-form-urlencoded Content-Type, or even ajaxify using ServiceStack's ss-utils.js bindForm method, e.g:

    $("#theForm").bindForm();
    

提交回复
热议问题