0

i have button that when you click on it it run ajax that send id to controller now controller do something and its ok no error in server side,, but in the action in controller i have code with restsharp that send request to rest web service its work fine too(), but this part() is in foreach and run for 5 or ... time and when i do this things sometime ajax takes to long and the error part of ajax code is run what should i do ?

ajax code:

             $(document).on("click", "#btn-submit", function () {
                    $.ajax({
                        type: 'POST',
                        url: '/Panel/CheckRefOrderCode',
                        data: JSON.stringify({
                            factorrefid: $("#ref-check").val()
                        }),
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (result) {
                            if (result.DntSuccess) {
                            } else {
                            }
                        },
                        error: function () {
                        }
                    });
            });

action code

                        foreach(string s in str)
                    {
                        var client = new RestClient("http://**.com/api/v1/orders/status?support_code=71GD4A");
                        var request = new RestRequest(Method.POST);
                        request.AddHeader("token", "15befa43");
                        IRestResponse response = client.Execute(request);

                        RefOrderJsonViewModel.RefOrderJson reforderbackJson =
                            JsonConvert.DeserializeObject<RefOrderJsonViewModel.RefOrderJson>(response.Content);

                        if (reforderbackJson.status.ToLower() == "ok")
                        {
                            performed += reforderbackJson.data.performed;
                            order_status += reforderbackJson.data.order_status + "^";
                        }
                    }

and i add this to web.config

    <httpRuntime executionTimeout="100000000" maxRequestLength="262144" />
keyone2693
  • 619
  • 1
  • 4
  • 17

2 Answers2

0

Add a timeout to the ajax call:

 $(document).on("click", "#btn-submit", function () {
                $.ajax({
                    type: 'POST',
                    url: '/Panel/CheckRefOrderCode',
                    data: JSON.stringify({
                        factorrefid: $("#ref-check").val()
                    }),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    **timeout: 10000 // sets timeout to 10 seconds**
                    success: function (result) {
                        if (result.DntSuccess) {
                        } else {
                        }
                    },
                    error: function () {
                    }
                });
        });

Check out this post on how to handle timeout error.

Community
  • 1
  • 1
Wei
  • 724
  • 9
  • 10
  • its not ajax time out problem..i see the error and the ajax not return a timeout error....and server side : the asp after ajax error still doing it job according to fidller – keyone2693 Jul 23 '16 at 13:32
0

the problem still is on;

but i use this

    Parallel.ForEach(myEnumerable, obj =>
{
   // ... 
});

instead regular

foreach

and do the body of foreach in parallel, and the time is decreasing, the problem solved

keyone2693
  • 619
  • 1
  • 4
  • 17