尽管代码返回正确的数据,但Ajax调用返回空响应

时间:2021-10-09 07:42:10

I'm doing something like:

我做的事情如下:

 $.ajax({
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    url: '/Modal/getModalInfo',
    data: '{"machine":"' + machine + '"}',
    success: function (response) {
        modalData = response;
        fillModal();
    }
});

If I debug the C# and check the response being sent back to the JS, the data looks completely correct.

如果我调试C#并检查发送回JS的响应,则数据看起来完全正确。

The reponse in the Ajax call is just:

Ajax调用中的响应只是:

response = Object {}

and completely empty. I am doing several other Ajax calls in the exact same way that are executing correctly.

并且完全是空的。我正在以正确执行的相同方式执行其他几个Ajax调用。

Edit:

As far as what the C# is returning to Ajax:

至于C#返回Ajax的内容:

        try {
            return Json(allInfo, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex) {
            return new JsonResult();
        }

"allinfo" looks like:

“allinfo”看起来像:

    AllTheInfo allInfo = new AllTheInfo();


    class AllTheInfo {
    List<DownTimes> dList;

    internal List<DownTimes> DList {
        get { return dList; }
        set { dList = value; }
    }
    List<PartsMade> pList;

    internal List<PartsMade> PList {
        get { return pList; }
        set { pList = value; }
    }

    PartsTotals pTotal;

    internal PartsTotals PTotal {
        get { return pTotal; }
        set { pTotal = value; }
    }
}

The classes that are referenced inside that class are other custom classes I've made.

在该类中引用的类是我所做的其他自定义类。

Edit: I also modified my Ajax call to catch errors and it doesn't seem to think there are any errors, again, just an empty success response.

编辑:我还修改了我的Ajax调用以捕获错误,它似乎没有认为有任何错误,再次,只是一个空的成功响应。

Edit again: I've now attempted to convert the object I'm returning to a Json string within C# using the following:

再次编辑:我现在尝试使用以下内容将我正在返回的对象转换为C#中的Json字符串:

        string jsontext = "";
        try {
            jsontext = JsonConvert.SerializeObject(allInfo);

            System.IO.File.WriteAllText(path, jsontext);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }

The problem now is that the call to JsonConvert is completing successfully (no error being caught) but the string is equal to "{}"

现在的问题是对JsonConvert的调用成功完成(没有捕获错误)但字符串等于“{}”

I'm not sure why this would be interpreted as successful.

我不确定为什么这会被解释为成功。

2 个解决方案

#1


0  

I've seen this in the past when the client-side fails to parse the JSON coming back from the server due to syntax errors, additional text in the response (e.g. HTML tags etc.). Best bet is to use something like Fiddler (http://www.telerik.com/fiddler) or similar proxy to see exactly what's coming back in the response (not just what you're returning from the C# side as there may be additional filters, modules etc. affecting the output after it leaves your server-side method).

我在过去看到过这种情况,当客户端由于语法错误,响应中的其他文本(例如HTML标签等)而无法解析从服务器返回的JSON。最好的办法是使用类似Fiddler(http://www.telerik.com/fiddler)或类似代理的东西,以确切了解响应中的内容(不仅仅是你从C#端返回的内容,因为可能还有其他内容)影响输出的过滤器,模块等在离开服务器端方法之后)。

#2


0  

The 5 different classes I'm using to create the final object I'm passing back to Ajax have getters and setters, one of the classes had their getters/setters marked like:

我用来创建我传递回Ajax的最终对象的5个不同的类有getter和setter,其中一个类的getter / setter标记为:

internal List<PartsMade> PList {
        get { return pList; }
        set { pList = value; }
    }

and should have been marked:

并且应该标记为:

public List<PartsMade> PList {
        get { return pList; }
        set { pList = value; }
    }

This was causing the Json serializers in C# to be incapable of accessing the classes through their getters.

这导致C#中的Json序列化程序无法通过其getter访问类。

The output was correct after changing the getter's access modifier to public.

将getter的访问修饰符更改为public后输出正确。

#1


0  

I've seen this in the past when the client-side fails to parse the JSON coming back from the server due to syntax errors, additional text in the response (e.g. HTML tags etc.). Best bet is to use something like Fiddler (http://www.telerik.com/fiddler) or similar proxy to see exactly what's coming back in the response (not just what you're returning from the C# side as there may be additional filters, modules etc. affecting the output after it leaves your server-side method).

我在过去看到过这种情况,当客户端由于语法错误,响应中的其他文本(例如HTML标签等)而无法解析从服务器返回的JSON。最好的办法是使用类似Fiddler(http://www.telerik.com/fiddler)或类似代理的东西,以确切了解响应中的内容(不仅仅是你从C#端返回的内容,因为可能还有其他内容)影响输出的过滤器,模块等在离开服务器端方法之后)。

#2


0  

The 5 different classes I'm using to create the final object I'm passing back to Ajax have getters and setters, one of the classes had their getters/setters marked like:

我用来创建我传递回Ajax的最终对象的5个不同的类有getter和setter,其中一个类的getter / setter标记为:

internal List<PartsMade> PList {
        get { return pList; }
        set { pList = value; }
    }

and should have been marked:

并且应该标记为:

public List<PartsMade> PList {
        get { return pList; }
        set { pList = value; }
    }

This was causing the Json serializers in C# to be incapable of accessing the classes through their getters.

这导致C#中的Json序列化程序无法通过其getter访问类。

The output was correct after changing the getter's access modifier to public.

将getter的访问修饰符更改为public后输出正确。