无法使用Ajax调用函数 [英] Can't call function using ajax

查看:96
本文介绍了无法使用Ajax调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ajax调用一个函数,但是它没有响应.

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

这是代码:

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

这是函数:

 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 !");

    }
 }

注意:当我尝试直接调用它时,它可以工作,但是当使用ajax时却没有结果……我该如何解决这个问题?

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

推荐答案

确保jQuery正常运行

首先,确保您的jQuery代码正常工作并被引用.您需要包含对库之前的引用,以调用任何与jQuery相关的代码:

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> 

如果要执行GET ...

If you want to perform a GET...

您在 ajax() 调用中指定正在执行POST通过type属性发出请求,但您的控制器操作明确希望通过[HttpGet]属性装饰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.

您可以通过将type属性从POST更改为GET来解决此问题(或者可以将其完全删除,因为默认值为GET):

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",

如果要执行POST ...

If you want to perform a POST...

或者,如果您想实际执行POST,则只需保留现有代码并使用[HttpPost]属性即可:

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
}

这篇关于无法使用Ajax调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆