jQuery Ajax请求在Asp.net MVC 4中给移动设备带来了错误

时间:2022-04-14 09:14:46

I am using jquery 1.11.1 with Asp.net MVC I am requesting a $.ajax request to my MVC Controller Action and it is working fine on desktop but giving error on mobile version

我正在使用带有Asp.net MVC的jquery 1.11.1我正在向我的MVC Controller Action请求一个$ .ajax请求它在桌面上工作正常但在移动版本上出错

My jQuery Ajax request is as follows:

我的jQuery Ajax请求如下:

$.ajax({
       method: 'GET',
       url: ajaxurl + 'GetListingByAjax',
       contentType: 'application/json; charset=utf-8',
       data: JSON.stringify({
           pagenumber: pageno,
           currenturl: location.href
       }),
       dataType: 'json',
       success: function (data) {
           if (data.length > 0) {
              $('#LoadMoreListings').show();
              var FullHTML = GetListingHTML(data);
              $('#loading').remove();               
              pageno++;
           }
           else {
              $('#LoadMoreListings').hide();
              $('#loading').text('No more listings to display');
           }
       },
       error: function (a,b,c) {
            alert('Exeception Description : '+c);
       }
   });

And My Controller Action is

我的控制器动作是

public JsonResult GetListingByAjax(int pagenumber, string currenturl = "")
{
    var ListingItemsList = new Listing().GetListingAjax(currenturl, pagenumber, 20, 0);
    return Json(ListingItemsList, JsonRequestBehavior.AllowGet);
}

This is working fine on desktop and when I run it on mobile it gives me Internal Server Error then I checked the response in Chrome Emulator then it shows following error

这在桌面上工作正常,当我在移动设备上运行时它会给我内部服务器错误,然后我在Chrome模拟器中检查了响应然后它显示以下错误

The parameters dictionary contains a null entry for parameter 'pagenumber' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult GetListingAjax(Int32, System.String)

参数字典包含方法'System.Web.Mvc.JsonResult GetListingAjax(Int32,System.String)的非可空类型'System.Int32'的参数'pagenumber'的空条目

Then I tried by changing method GET to POST but the error on mobile was same and on desktop it was fine

然后我尝试将方法GET更改为POST,但移动设备上的错误是相同的,在桌面上它很好

Can anyone help me please am I doing something in wrong way or i need to add anything extra in this code to be run on mobile

任何人都可以帮助我,我是以错误的方式做某事,或者我需要在此代码中添加额外的东西以便在移动设备上运行

Please help me out thanks in advance

请提前帮助我

1 个解决方案

#1


You are missing the pagenumber parameter in your ajax URL:

您缺少ajax URL中的pagenumber参数:

url: ajaxurl + 'GetListingByAjax',

should be something like

应该是这样的

url: ajaxurl + 'GetListingByAjax/2',

In that way the controller gets to make use of the parameter pagenumber you've told it to require.

通过这种方式,控制器可以使用您告诉它需要的参数pagenumber。

#1


You are missing the pagenumber parameter in your ajax URL:

您缺少ajax URL中的pagenumber参数:

url: ajaxurl + 'GetListingByAjax',

should be something like

应该是这样的

url: ajaxurl + 'GetListingByAjax/2',

In that way the controller gets to make use of the parameter pagenumber you've told it to require.

通过这种方式,控制器可以使用您告诉它需要的参数pagenumber。