簡體   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