jQuery success callback called with empty response when WCF method throws an Exception

前端 未结 5 1849
既然无缘
既然无缘 2021-02-05 11:55

I\'m tearing my hair out over this one, so bear with me (it\'s a long post).

Basic Info

  • ASP.NET 3.5 with WCF service in ASP.NET compatibility mode
5条回答
  •  一个人的身影
    2021-02-05 12:04

    Have you looked at JSON.NET? I was using it to convert objects in c# to JSON friendly strings then passing it back across the wire to my client where I parsed it into a JSON object. In the end I got rid of it and went to JSON2 for stringify. Here is my ajax call I use:

    function callScriptMethod(url, jsonObject, callback, async) {
    
        callback = callback || function () { };
        async = (async == null || async);
    
        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: url,
            data: JSON.stringify(jsonObject),
            dataType: 'json',
            async: async,
            success: function (jsonResult) {
                if ('d' in jsonResult)
                    callback(jsonResult.d);
                else
                    callback(jsonResult);
            },
            error: function () {
                alert("Error calling '" + url + "' " + JSON.stringify(jsonObject));
                callback([]);
            }
        });
    }
    

提交回复
热议问题