【发布时间】:2023-03-12 12:40:02
【问题描述】:
我有一个 Web Api 项目,首先我用一个 HttpPost 方法创建了一个控制器,然后它工作正常。但是当我添加了另一个 HttpPost 方法时,没有人在工作,但是当我删除任何人时,另一个人正在工作。我的代码是。
Web API 控制器:
public class ForumPost
{
public int ProjectId { get; set; }
public int CourseId { get; set; }
public int SubjectId { get; set; }
public int TopicId { get; set; }
public int ContentId { get; set; }
public string Heading { get; set; }
public string Description { get; set; }
public int UserId { get; set; }
}
[HttpPost]
[ActionName("AddForumPost")]
public string AddForumPost([FromBody]ForumPost _ForumPost)
{
string strResult = "N";
using (ICA.LMS.Service.Models.Entities db = new Entities())
{
}
return strResult;
}
public class Comment
{
public int ForumId {get;set;}
public string Response { get; set; }
public int ParentId { get; set; }
public int LevelId { get; set; }
public string CreatedBy { get; set; }
}
[HttpPost]
[ActionName("AddComment")]
public string AddComment([FromBody]Comment cmt)
{
string strResult = "N";
using (ICA.LMS.Service.Models.Entities db = new Entities())
{
}
return strResult;
}
jQuery 调用:
var comment = { ForumId: fId, Response: res, ParentId: pId, LevelId: lId, CreatedBy: uId };
jQuery.support.cors = true;
$.ajax({
url: 'http://localhost:1900/api/ForumApi',
type: 'Post',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data:comment,
dataType: 'json',
async: false,
success: function (data) {
var efId = $('#EncForumId').val();
if (data == "Y")
location.replace('../Forum/ForumDiscussion?id=' + efId);
else
myAlert('Unable to Post. Try again!');
},
error: function (e) {
myAlert(JSON.stringify(e));
}
});
错误:-
**{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:1900/api/ForumApi'."}**
【问题讨论】:
标签: jquery asp.net asp.net-mvc-4 asp.net-web-api