1. Enable cross-domain submissions
<system.webserver> <Httpprotocol> <customheaders> <Addname= "Access-control-allow-origin"value="*" /> <Addname= "Access-control-allow-methods"value= "GET, POST" /> </customheaders> </Httpprotocol> </system.webserver>
(function () { var domain = ' http://localhost:5000/'; var apiurl = { function (action) { return domain + ' postone/ ' + action; }, function (action) { return domain + ' GetOne /' + action; } } = Apiurl;}) ();
2.Get in the same way as MVC
Get Way Foreground code
/* * cross-domain GET requests * 1 parameters */ $.getjson (Apiurl.getone ("Testone"), { 234}). Done (function (data) { alert (data);}); /* * cross-domain GET requests * 2 parameters */ $.get (Apiurl.getone ("Testtwo"), { ' abc ', age : one function (data) { alert (data);});
Get Way Background code
/** 1.WebApi Controller method only supports one format request, default is GET request, if one of the specified post, other request also post* 2.WebApi controller method does not support get request by default, * throws exception: the requested Resour Ce does not support HTTP method ' GET '. * Requires manual designation * Frombody: Specify parameter from external request * Fromuri: Specify parameter from URL*///foreground GET request gets a parameter[HttpGet] Public stringTestone (stringname) { return string. Format ("name: {0}", name);}//foreground GET request gets 2 parameters[HttpGet] Public stringTesttwo (stringNameintAge ) { return string. Format ("name: {0}, Age: {1}", name, age);}
3.POST is not the same as MVC, only get the first parameter, need to make Frombody flag
Front Code
/** cross-domain POST request * No parameters, get list*/$.post (Apiurl.getone ("Postnull"), {}, function(data) {console.info (data); Alert (data[0].name);});/** cross-domain request post* A parameter, cannot specify name of key * Note parameter specify JSON string*/$.post (Apiurl.getone ("Postone"), { ': ' Zhang San ' }, function(data) {alert (data); });/** cross-domain request post* 2 parameters----failed*/$.post (Apiurl.getone ("Posttwo"), {name:' Zhang San ', Age:12}, function(data) {alert (data); });/** cross-domain request post* 1 Object Parameters*/$.post (Apiurl.getone ("Postthree"), {name:' Zhang San ', Age:12 }, function(data) {alert (data); }); $.ajax ({Url:apiUrl.getOne ("Postthree"), data: {name:' Zhang San ', Age:12}, type:' Post ', Success:function(data) {alert (data); }});
The corresponding background code
/** The 1.WEBAPI controller method supports post requests by default * 2. By default, one controller supports only one POST request * Specifies the controller, when no action is specified, there are multiple requests in the current controller, *: Throws an exception, Multiple actions were found that match the request* 3.post method can only accept one parameter and must be placed in Frombody with Frombody feature identification*///POST Request no parameter method, return result JSON format[Httpget][httppost] Publiclist<Object>Postnull () {List<Object> list =Newlist<Object>() { New{name="Zhang San"}, New{name="John Doe", age= One}, }; returnlist;}//POST request one parameter Public stringPostone ([Frombody]stringname) { return string. Format ("name: {0}", name);}/*POST Request 2 parameters, * participate in external interactive action only accept one parameter * does not support multiple frombody* throws exception: "Can ' t bind multiple parameters (' name ' and ' age ') to the requ EST ' s content. "*/ Public stringPosttwo ([Frombody]stringName, [Frombody]intAge ) { return string. Format ("name: {0}, Age:", name, age);}//POST request 1 Object Parameters Public stringPostthree ([frombody] Student stu) {returnStu. ToString ();}
Resources:
Http://www.cnblogs.com/babycool/p/3922738.html
ASP. NET WebAPI get commit, post commit processing