0

Locally code working fine but when uploaded to server showing 500 [Internal server error] I have tried with single parameter but not working on live environment

  • Parameters are in JSON.stringify(itemlist)

here is my ajax code:

 var itemlist = new Array();
 var item = {
        Fnumber: Fnumber, Snumber: Snumber, Flag: Flag, Page: _pageName, bulkupdate: false, sortText: $("#hdnsorting").val(), excel: false, filtCompany: $("#hdnfilterCompanies").val(), ShowDDLvalue: ddlValue, url: "localhost"
    }
    itemlist.push(item);
 $.ajax({
    url: '/CompanyContact/GetContacts',
    data: JSON.stringify(itemlist),
    dataType: "json",
    type: "POST",
    cache: false,
    contentType: "application/json; charset=utf-8",
    success: function (data) {}
});

here is my controller method which is having FilterParameters model for parameters list:

  [HttpPost]
    public JsonResult GetContacts(List<FilterParameters> item)
    {
        var List = _api.PostRequest<List<FilterParameters>, Contacts>(_baseAPIurl + "contacts", item, Convert.ToString(Session["AccessToken"]));
        return Json(List.Result, JsonRequestBehavior.AllowGet);
    }

and here is my API call:

 public async Task<List<TOut>> PostRequest<TIn, TOut>(string uri, TIn content, string token)
    {
        List<TOut> DataList = new List<TOut>();
        try
        {
            using (var client2 = new HttpClient())
            {
                // token authorization
                client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                client2.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var serialized = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");

                var postTask = client2.PostAsJsonAsync<TIn>(uri, content);
                postTask.Wait();

                var result = postTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<TOut[]>();
                    readTask.Wait();
                    var Data = readTask.Result;
                    DataList = Data.ToList();
                    return DataList;
                }
                else
                {
                    return DataList = null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return null;
        }
    }

Also tried with sending all parameters of FilterParameters model object but failed. thanks in advance.

6
  • Can you tell more details about the 500 internal server error. In this way it would be easier to get to the root of issue Commented Aug 23, 2020 at 11:19
  • Hi Muhammad, While calling controller method from ajax, It is saying: Failed to load resource: the server responded with a status of 500 [Internal server error] and sometimes getting 404 (Not Found) - Somewhere between controller and PostRequest method not working
    – 3338_SON
    Commented Aug 23, 2020 at 11:26
  • if server is responding with a status of 404 it means it is not able to find your API that means you need to recheck you url which is building from client side Commented Aug 23, 2020 at 11:30
  • Or do your server knows the _baseapiurl or it is being set to the local one Commented Aug 23, 2020 at 11:32
  • I have set _baseapiurl in web config. URL is showing correct. Do we need some settings for Live API's ?
    – 3338_SON
    Commented Aug 23, 2020 at 11:35

1 Answer 1

0

You experience error code 500 when there are Non-Nullable elements in the Model and you are missing a particular element in your post. So make sure you are mentioning all the elements in the "FilterParameters" object. Also in your case, I think, there is no need to STRINGIFY the json array. send the json array directly.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.