问题
A naive question about web service.
I need to implement a complicate query. The client need to pass in many parameters to the server, and the server will send back response will many data fields.
What type of web service is suitable for this? I know that RESTful POST method is meant to "create" an object, but can I use POST for implementing this?
Or SOAP is better for this?
回答1:
Here it is.
//for the Controller
public JsonResult GetData(string param1, string param2)
{
List<YourModecClass> data = new List<YourlModelClass>();
//Mockup data only...you should get the data from DB source
data = new List<YourModelClass>();
data.Add(new YourModelClass() { Region = "", Value_TY = 0});
data.Add(new YourModelClass() { Region = "", Value_TY = 0 });
return Json(data, JsonRequestBehavior.AllowGet);
}
//jQuery
function getServerData() {
var entity = {
param1: param1 //--> ths is a variable
param2: "value" //--> hardcoded
}
var parameter = JSON.stringify(entity);
$.ajax({
type: "POST",
url: url + "/GetData",
data: parameter,
dataType: "json",
contentType: "application/json",
async: true,
beforeSend: function () {
},
success: function (response, status, xhr) {
yourJavascriptVariable = response;
doSomethingWithreceivedDataAbove();
},
error: function (xhr, status, error) {
debugger;
}
});
来源:https://stackoverflow.com/questions/27608850/what-web-service-to-use-for-complicate-query