[英]The resource cannot be found. MVC4
I am trying to do ajax call to my controller that uses HttpPost method 我正在尝试对使用HttpPost方法的控制器进行ajax调用
AJAX CALL AJAX CALL
$(document).ready(function () {
$('#contactDiv ').click(function() {
var number = $(this).find('.ContactNumber').text();
var dataJson = {"contactNumber": number};
// var dataJson = {"contactNumber": number };
$.ajax({
type: "POST",
url: "../contactWeb/messages",
data: JSON.stringify(dataJson),
//data: dataJson,
contentType: "application/json",
cache: false,
success: function (msg) {
//msg for success and error.....
// alert(msg);
return true;
}
});
location.href = '@Url.Action("messages")';
});
});
CONTROLLER CONTROLLER
[HttpPost] [AllowAnonymous] public ActionResult messages(string contactNumber) { // return the app messages of a pirticular user on contact basis Int64 userID = Convert.ToInt64(Session["userId"]); [HttpPost] [AllowAnonymous] public ActionResult messages(string contactNumber){//返回基于联系人的特定用户的app消息Int64 userID = Convert.ToInt64(Session [“userId”]); try { List messagesModel = new List(); try {List messagesModel = new List(); IMessages MessageObject = new MessagesBLO(); IMessages MessageObject = new MessagesBLO();
messagesModel = MessageObject.GetAllMessagesWeb(userID , contactNumber);
ViewData["Data"] = messagesModel;
}
catch (Exception e)
{
}
return View();
// string msg = "Error while Uploading....";
//return Json(msg, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult messages()
{
return View();
// string msg = "Error while Uploading....";
//return Json(msg, JsonRequestBehavior.AllowGet);
}
VIEW "messages" is 查看“消息”是
@model OyeAPI.Models.PhoneMessages
@{
ViewBag.Title = "messages";
List<Model.MessagesModel> ListMessages = (List<Model.MessagesModel>)ViewData["Data"];
Int64 MessageId = 0;
Int64 SenderId = 0;
Int64 ReceiverId = 0;
string Message = null;
string Date = null;
string SendReceive = null;
int MessageType = 0;
}
<div class="main_Container">
<div>
<table>
<tr>
<td>Message ID</td><td>SenderID</td><td>recieverID</td><td>message</td><td>date</td>
</tr>
@foreach (var item in ListMessages)
{
MessageId = item.MessageId;
SenderId = item.SenderId;
ReceiverId = item.ReceiverId;
Message = item.Message;
Date = item.Date.ToShortDateString();
SendReceive = item.SendReceive;
MessageType = item.MessageType;
<tr>
<td>@MessageId</td>
<td>@SenderId</td>
<td>@ReceiverId</td>
<td>@Message</td>
<td>@Date</td>
</tr>
}
</table>
</div>
</div>
it returns Resource not found error which is very annoying to me because i tried to delete and rebuilt the view but still unable to solve it.and one thing else what i want to do in this code is that with ajax call i send the contactNo to controller on the basis of which it checks the corresponding messages of this contactno number and pass it to its view messages. 它返回资源未找到错误,这对我来说非常烦人,因为我试图删除并重建视图但仍然无法解决它。还有一件事我想在这段代码中做的是用ajax调用我发送contactNo到控制器在此基础上检查此contactno号码的相应消息并将其传递给其查看消息。 so when the ajax call response is successfully done then it will redirect to messages view. 因此,当ajax调用响应成功完成后,它将重定向到消息视图。
I think this line in your javascript that makes you get a 404 - not found error : 我认为你的javascript中的这一行会让你得到一个404 - 找不到的错误:
location.href = '@Url.Action("messages")';
I assume that it tries to redirect to the very same controller which code you write in the question, which is marked with [HttpPost]
. 我假设它试图重定向到你在问题中编写的代码的同一个控制器,该代码用[HttpPost]
标记。
So what happen is, you try to GET
a resource that is marked as [HttpPost]
. 所以会发生什么,你尝试GET
标记为[HttpPost]
的资源。
The solution is to remove the [HttpPost]
attribute, or provide another messages
action in that controller that accept a GET
request. 解决方案是删除[HttpPost]
属性,或在该控制器中提供另一个接受GET
请求的messages
操作。
Make sure there is no any custom authorize attribute set for Controller. 确保没有为Controller设置任何自定义授权属性。
If it is, mark this particular controller with [AllowAnonymous] attribute. 如果是,请使用[AllowAnonymous]属性标记此特定控制器。
[HttpPost]
[AllowAnonymous]
public ActionResult messages(string contactNumber)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.