jQuery .ajax调用永不调用方法 [英] jQuery .ajax call never calling method

查看:117
本文介绍了jQuery .ajax调用永不调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此 SO发布我学习了如何从AJAX调用中获取返回值:

In this SO post I learned how to get a return value from an AJAX call:

function CallIsDataReady(input) {
            $.ajax({
                url: "http://www.blah.com/services/TestsService.svc/IsDataReady",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: input,
                dataType: "json",
                success: function (data) {
                    if (!data) {
                        setTimeout(function (inputInner) { CallIsDataReady(inputInner); }, 1000);
                    }
                    else {
                        //Continue as data is ready
                        callUpdateGrid(input);
                    }
                }
            });
        }

        $(document).ready(function () {
            var input = { requestGUID: "<%=guid %>" };

            CallIsDataReady(input);

        });

此函数调用其Web服务,但确实返回true.问题在于,在下面的callUpdateGrid中,日志记录表明未从$ .ajax调用中调用Web服务方法:

This function calls its web service wich does return true. The problem is that inside the following callUpdateGrid, the logging shows that that web service method is not getting called from the $.ajax call:

function callUpdateGrid(input) {
    console.log(input);
    $.ajax({
        url: "http://www.blah.com/services/TestsService.svc/GetContactsDataAndCountbyGUID",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        data: input,
        dataType: "json",
        success: function (data) {
            var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                        console.log(data);
                        mtv.set_dataSource(data.d.Data);
                        mtv.dataBind();
                    }
    });
}

有人知道怎么了吗?

推荐答案

始终包含错误处理程序功能作为传递给$ .ajax的选项之一是一个好主意.例如,在成功功能之后添加以下代码:

It is always a good idea to include an error handler function as one of the options passed to $.ajax. For example, add this code after your success functions:

    ,
    error: function(jqXHR, textStatus, errThrown) {
        console.log("AJAX call failed");
        console.log(errThrown);
    }

如果$ .ajax调用未成功,它将至少记录一些信息.

That will log at least a bit of information if the $.ajax call doesn't succeed.

编辑 根据您的评论,此日志 SyntaxError: Invalid character

EDIT According to your comment, this logs SyntaxError: Invalid character

事实上,我现在看到您正在将普通的JavaScript对象作为传递给$ .ajax的data选项,但在dataType字段中指示它是JSON对象.您实际上需要亲自将输入对象转换为JSON,如下所示:

And in fact, I now see that you are giving a plain JavaScript object as the data option passed to $.ajax, but indicating that it is a JSON object in the dataType field. You need to actually convert the input object into JSON yourself, like so:

    data: JSON.stringify(input),
    dataType: 'json',

或者,您可以首先将input格式化为JSON对象,如下所示:

Alternatively, you could simply format input as a JSON object in first place, like so:

var input = { "requestGUID": "<%=guid %>" };

在这种情况下,字段名称requestGUID周围的引号足以为您提供JSON对象.

The quotes around the field name requestGUID are sufficient, in this case, to give you a JSON object.

这篇关于jQuery .ajax调用永不调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆