I am trying to do ajax call to my controller that uses HttpPost method
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
[HttpPost] [AllowAnonymous] public ActionResult messages(string contactNumber) { // return the app messages of a pirticular user on contact basis Int64 userID = Convert.ToInt64(Session["userId"]); try { List messagesModel = new List(); 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. so when the ajax call response is successfully done then it will redirect to messages view.
I think this line in your javascript that makes you get a 404 - not found error :
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]
.
So what happen is, you try to GET
a resource that is marked as [HttpPost]
.
The solution is to remove the [HttpPost]
attribute, or provide another messages
action in that controller that accept a GET
request.
Make sure there is no any custom authorize attribute set for Controller.
If it is, mark this particular controller with [AllowAnonymous] attribute.
[HttpPost]
[AllowAnonymous]
public ActionResult messages(string contactNumber)
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.