.net - Web Api 2.0 在邮递员中工作但不在 HTML 页面中
问题描述
这是我在.Net中用于web api 2.0的控制器代码当我使用邮递员时它工作正常但是当我从HTML页面调用它时我得到错误No HTTP resource was found that matches the request URI
我做错了什么?
控制器代码
[HttpPost]
public IHttpActionResult GetCustomerName (string num)
{
return Json(new
{
success = true,
message = "My Name "+ num,
});
}
HTML 代码
$.ajax({
url: "http://localhost:10000/api/CFP/GetCustomerName",
type: "POST",
data: {
'num': '123'
},
contentType: 'application/json; charset=utf-8',
success: function (response) {
alert(response.message);
},
error: function (xhr, error, thrown) {
alert(xhr.responseText);
}
});
解决方案
$.ajax({
url: "http://localhost:10000/api/CFP/GetCustomerName",
type: "POST",
data: JSON.stringify({
'num': '123'
}),
contentType: 'application/json; charset=utf-8',
success: function (response) {
alert(JSON.stringify(response));
},
error: function (xhr, error, thrown) {
alert(xhr.responseText);
}
});
您可以通过对数据进行字符串化来尝试上面的代码吗
您可以从邮递员本身创建正确的代码:
只需 clikc 代码并找到等效的 ajax 代码
推荐阅读
- javascript - 反应页面不从顶部呈现
- wordpress - 无法使用 Elementor 编辑 Wordpress 网站(“更新失败”)
- javascript - 与 async.parallel 斗争
- oracle11g - 无法访问表空间的 Oracle 用户是否可以将值插入到放置在该表空间中的表中?
- php - 如何在laravel中存储多个关系
- python - 如何根据 python 中的用户输入检查从数组中随机生成的值?
- node.js - 为什么 .pre find 查询在这种情况下返回 undefined?
- r - 对列字符串的唯一组合进行子集化
- php - php - 从逗号分隔的值范围中获取最小值和最大值
- shopify - Complete Checkout API 给出错误“无法在没有任何交易的情况下完成结帐 shopify”