標籤:style blog http java color get
按照正常的寫法,總是出現404錯誤,研究了很久,在這裡找到瞭解決方案:http://buxuxiao.com/article/using-jquery-to-post-frombody-parameters-to-web-api
現在總結一下,
單個參數的情況下:
1、後台參數正確的寫法如下:
[Route("Services/{controller}/{action}")][HttpPost][HttpGet]public string UploadTransportNetworkAlarmChat([FromBody]string obj){
...}
必須在參數列表前面加上[FormBody]標籤。
2、前台的參數,仍然以key-value方式添加,但KEY的名字必須置為空白:
$.ajax({ type: "POST", url: "../Services/ChatTest/UploadTransportNetworkAlarmChat", data: {"":"helloWorld"}, dataType: ‘JSON‘,})
多個參數的情況下,
1、後台需要定義實體類,參數列表中不用增加[FromBody]標籤,如下:
public class tempObj { public string imgBase64 { get; set; } public string name { get; set; } }
接收 方法:
[Route("Services/{controller}/{action}")] [HttpPost] [HttpGet] public string UploadTransportNetworkAlarmChat(tempObj obj) {}
2、前台仍然使用JSON
var TempObj = { "imgBase64": image, "name": "helloWorld" } $.ajax({ type: "POST", url: "../Services/ChatTest/UploadTransportNetworkAlarmChat", data: TempObj, dataType: ‘JSON‘, }) .done(function (data) { alert("Data Loaded: " + data); });