weixin_33738555 2016-04-28 18:27 采纳率: 0%
浏览 42

无法使用ajax调用函数

I'm trying to call a function using ajax, but it's not responding.

Here is the Code:

<p id="myElem" class="alert-danger" style="display:none"></p>

<button title="Remove" data-id="@item.Book.Book_id" class="Remove btn btn-group-sm red" style="float:initial">Remove</button>

<script type="text/javascript">
    $('.Remove').click(function () {

        var myId = $(this).data('id');

        $.ajax({
            type: "POST",
            url: '@Url.Action("Remove", "ReadingNow")?Book_id=' + myId,
            success: function (response) {
                $('#myElem').html(response).fadeIn('slow');
                $('#myElem').delay(8000).fadeOut('slow');
            },
            failure: function (response) {

                alert("Failure");
            }
        });
    });
    </script>

and here is the function :

 public class ReadingNowController : Controller
{
[HttpGet]
    public ActionResult Remove(int? Book_id)
    {
        if (User.Identity.IsAuthenticated)
        {
            var x = User.Identity.GetUserId();
            var IsExists = db.ReadingNow.Where(t => t.Book_Id == Book_id && t.User_Id == x).FirstOrDefault();

            if (IsExists != null)
            {

                db.ReadingNow.Remove(IsExists);
                int state = db.SaveChanges();
                if (state == 1)
                {
                    return Content("Book Removed From Your Reading Now List !");

                }
            }                

        }
        return Content("Error !");

    }
 }

Note: When I tried to call it directly it works, but when used ajax I got no result... How can I solve this problem?

  • 写回答

3条回答 默认 最新

  • 程序go 2016-04-28 18:29
    关注

    Ensure That jQuery Is Working

    Firstly, ensure that your jQuery code is working properly and is referenced. You'll need to include a reference to the library prior to calling any jQuery-related code :

    <!-- Example Reference -->
    <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
    <script>
        $(function(){
              // Place your code here
        });
    </script> 
    

    If you want to perform a GET...

    You are specifying within your ajax() call that you are making a POST request via the type attribute, but your Controller Action is explicitly expecting a GET via the [HttpGet] attribute decorating your action.

    You can resolve this by changing your type attribute from POST to GET (or you could remove it entirely as GET is the default):

    type: "GET",
    

    If you want to perform a POST...

    Alternatively, if you want to actually perform a POST, then just keep your existing code and use the [HttpPost] attribute instead :

    [HttpPost]
    public ActionResult Remove(int? Book_id)
    {
         // Code omitted for brevity
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题