繁体   English   中英

无法找到该资源。 MVC4

[英]The resource cannot be found. MVC4

我正在尝试对使用HttpPost方法的控制器进行ajax调用

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){//返回基于联系人的特定用户的app消息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);
}

查看“消息”是

@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>

它返回资源未找到错误,这对我来说非常烦人,因为我试图删除并重建视图但仍然无法解决它。还有一件事我想在这段代码中做的是用ajax调用我发送contactNo到控制器在此基础上检查此contactno号码的相应消息并将其传递给其查看消息。 因此,当ajax调用响应成功完成后,它将重定向到消息视图。

我认为你的javascript中的这一行会让你得到一个404 - 找不到的错误:

location.href = '@Url.Action("messages")';

我假设它试图重定向到你在问题中编写的代码的同一个控制器,该代码用[HttpPost]标记。

所以会发生什么,你尝试GET标记为[HttpPost]的资源。

解决方案是删除[HttpPost]属性,或在该控制器中提供另一个接受GET请求的messages操作。

确保没有为Controller设置任何自定义授权属性。

如果是,请使用[AllowAnonymous]属性标记此特定控制器。

    [HttpPost]
    [AllowAnonymous]
    public ActionResult messages(string contactNumber)
    {
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM