Ajax - 500 Internal Server Error

后端 未结 8 1120
难免孤独
难免孤独 2020-11-30 07:22

I am trying to learn AJAX for this project at work. I have a site that loads medications that a patient is taking.

I call this AJAX function up recursively so that i

8条回答
  •  执笔经年
    2020-11-30 07:55

    I had the same error. It turns out that the cause was that the back end method was expecting different json data. In my Ajax call i had something like this:

    $.ajax({
            async: false,
            type: "POST",
            url: "http://13.82.13.196/api.aspx/PostAjax",
            data: '{"url":"test"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        });
    

    Now in my WebMethod, inside my C# backend code i had declared my endpoint like this:

    public static string PostAjax(AjaxSettings settings)
    

    Where AjaxSettings was declared:

    public class AjaxSettings
    {
        public string url { get; set; }
    }
    

    The problem then was that the mapping between my ajax call and my back-end endpoint was not the same. As soon as i changed my ajax call to the following, it all worked well!

    var data ='{"url":"test"}';    
    $.ajax({
        async: false,
        type: "POST",
        url: "http://13.82.13.196/api.aspx/PostAjax",
        data: '{"settings":'+data+'}',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
    

    I had to change the data variable inside the Ajax call in order to match the method signature exactly.

提交回复
热议问题