使用Ajax时,服务器的状态为500(内部服务器错误) [英] Server responded with a status of 500 (Internal Server Error) when using Ajax

查看:411
本文介绍了使用Ajax时,服务器的状态为500(内部服务器错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个ajax电话:

Hello I have a ajax call:

 $.ajax({
        url: "/Orders/CheckIfExists",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        data: {
            catalogNumber: viewModel.catalogNumber,
            quantity: viewModel.quantity
        },
        error: function (data) {
            alert("wystąpił nieokreślony błąd " + data);
        },
        success: function (data) {
            if(data.ok)
            {
                alert(data.quantity)
            }
        }
    })
});

这是控制器方法:

public JsonResult CheckIfExists(string catalogNumber, int quantity)
    {
        List<Expression<Func<DeviceInstance, bool>>> where = new List<Expression<Func<DeviceInstance, bool>>>();
        where.Add(w=>w.DeviceUsage.UserId==1);
        where.Add(w => w.Project == null);
        where.Add(w => w.Device.CatalogNo == catalogNumber);
        var result = unitOfWork.deviceInstanceRepository.Get(where)
          .GroupBy(w => new
          {
              DeviceId = w.DeviceId,
              CatalogName = w.Device.CatalogNo,
          })
          .Select(s => new
          {
              Quantity = s.Sum(x => x.Quantity),
          }).First();
        if (result.Quantity >= quantity)
        {
            return Json(new { ok = true, quantity = result.Quantity});

        }
        return Json(new { ok = false });
    }

但是我总是收到内部500错误. 方法接收数据,所有计算都可以.我以示例的形式编写返回JSON. 我在哪里弄错了?

But I'm always getting Internal 500 error. Data is received by method and all calculations are ok. I compose return JSON as in example. Where I made a mistake?

推荐答案

默认情况下,ASP.NET MVC拒绝ajax GET请求,您必须通过显式设置

By default ASP.NET MVC rejects ajax GET requests, you have to allow it by explicitly setting JsonRequestBehavior to AllowGet:

return Json(new { ok = true, quantity = result.Quantity},
    JsonRequestBehavior.AllowGet);

这篇关于使用Ajax时,服务器的状态为500(内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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