无法从jquery调用简单的web API post方法

inkz8wg9  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(133)

首先,我无法将模型从jquery传递到我的web-api,所以我更改了代码,并使用GET和POST编写了一个简单的控制器

  1. using ContactInfo.Models;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.Http;
  5. using System.Web.Http.Cors;
  6. namespace ContactInfo.Controllers
  7. {
  8. [RoutePrefix("api/Contact")]
  9. public class ContactController : ApiController
  10. {
  11. List<ContactDto> contactList = new List<ContactDto>
  12. {
  13. new ContactDto { ContactId = 1, ContactName = "x", MobileNumber="1234567890" },
  14. new ContactDto { ContactId = 1, ContactName = "y", MobileNumber="1234567890" },
  15. };
  16. [HttpGet]
  17. [Route("GetProducts")]
  18. public IEnumerable<ContactDto> GetAllProducts()
  19. {
  20. return contactList;
  21. }
  22. [Route("AddProduct")]
  23. [HttpPost]
  24. public string Add(string name)
  25. {
  26. return name;
  27. }
  28. }
  29. }

字符串
我的HTML如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <!--<button id="Result" value="Submit" />-->
  10. <input id="Result" name="Result" type="button" value="Submit" />
  11. <script type="text/javascript">
  12. $(document).ready(function () {
  13. var name = 'hi';
  14. $("#Result").click(function () {
  15. $.ajax({
  16. url: 'http://localhost:51856/api/Contact/AddProduct',
  17. contentType: "application/json; charset=utf-8",
  18. dataType: "json",
  19. type: 'POST',
  20. data: name,
  21. success: function (response) {
  22. alert('hello');
  23. },
  24. failure: function (response) {
  25. alert(response.responseText);
  26. },
  27. error: function (response) {
  28. alert(response.responseText);
  29. }
  30. });
  31. });
  32. });
  33. </script>
  34. </body>
  35. </html>


我得到的错误是如下提交表格
“消息”:“找不到与请求URI 'http://localhost:51856/api/Contact/AddProduct'匹配的HTTP资源。",“MessageDetail”:“在控制器'Contact'上找不到与请求匹配的操作。"}

u91tlkcl

u91tlkcl1#

你做错了。有两种方法可以做到这一点。

  • 第一种方法-

添加模型类StudentModel.cs

  1. public class StudentModel
  2. {
  3. public string name { get; set; }
  4. }

字符串
然后接受该参数作为模型-

  1. [Route("AddProduct")]
  2. [HttpPost]
  3. public string Add(StudentModel model)
  4. {
  5. return "";
  6. }


在Jquery Request中->

  1. var postData = {};
  2. postData.name = "Tom";
  3. $("#Result").click(function () {
  4. $.ajax({
  5. url: '/api/Contact/AddProduct',
  6. contentType: "application/json; charset=utf-8",
  7. dataType: "json",
  8. type: 'POST',
  9. data: JSON.stringify(postData),
  10. success: function (response) {
  11. alert('hello');
  12. },
  13. failure: function (response) {
  14. alert(response.responseText);
  15. },
  16. error: function (response) {
  17. alert(response.responseText);
  18. }
  19. });
  20. });

  • 第二种方法->在URL url: '/api/Contact/AddProduct?name=' + name中传递name作为参数,并在Action AddProduct中接受参数作为字符串(不推荐,因为它是POST请求)
展开查看全部

相关问题