语法错误:jQuery $ .ajax调用中的字符无效? [英] Syntax Error: Invalid character in jQuery $.ajax call where?

查看:130
本文介绍了语法错误:jQuery $ .ajax调用中的字符无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一点jQuery调用WCF方法。方法调用成功到我可以看到它记录的程度, 返回布尔值true。但是,错误处理程序将返回CallIsDataReady中的AJAX调用失败和语法错误:无效字符。然后它不会将成功路径调用callUpdateGrid。我找不到无效字符。帮助!

I have the following bit of jQuery calling a WCF method. The method call succeeds to the extent that I can see it logging and it does return a Boolean true. However, the error handler is coming back with "AJAX call failed in CallIsDataReady" and "Syntax Error: Invalid character." It then does not take the success path call the callUpdateGrid. I can't find the Invalid character. Help!

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 {
                        console.log("data returned - calling callUpDateGrid");
                        //Continue as data is ready
                        callUpdateGrid(input);
                    }
                },
                error: function (jqXHR, textStatus, errThrown) {
                    console.log("AJAX call failed in CallIsDataReady");
                    console.log(errThrown);
                }
            });
        }

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

            CallIsDataReady(input);

        });

服务器端方法返回JSON,因为它是支持AJAX的Web服务:

Server side method returns JSON as it is an AJAX enabled Web service:

[OperationContract]
        [WebGet]
        public bool IsDataReady(string requestGUID)
        {
            bool isReady = Global.publicDataDictionary.Keys.Contains(requestGUID);

            using (savitasEntities2 db = new savitasEntities2())
            {
                DataRequestLog drl = new DataRequestLog();
                drl.registrationID = "";
                drl.request = "Is Ready=" + isReady;
                drl.connectionID = "";
                drl.created = System.DateTime.Now.ToUniversalTime();
                drl.direction = "tickler";
                drl.dataRequestGUID = requestGUID;
                db.DataRequestLogs.Add(drl);
                db.SaveChanges();
            }

            return isReady;
        }

编辑:第二种JavaScript方法是:

the 2nd JavaScript method is:

 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();
                },
                error: function (jqXHR, textStatus, errThrown) {
                    console.log("AJAX call failed in callUpdateGrid");
                    console.log(errThrown);
                }
            });
        }


推荐答案

因为你有你的代码工作但你仍然应该考虑这些事情。我不是JavaScript的专家,但我相信你调用方法的方式是不正确的。首先,您的 OperationContract WebGet ,因此您无需提供内容-Type 在您的请求中。当您使用 POST 方法发送大量数据时,通常会使用 Content-Type 。所以,据我所知,改变如下:

As you have got your code working but still you should consider these things in mind. I am not expert in JavaScript but I believe that the way you are calling method is incorrect. First of all, your OperationContract is WebGet, so you don't need to provide Content-Type in your request. Content-Type is normally used when you are sending large amount of data using POST method. So, according to me, change following:

[OperationContract]
[WebGet(UriTemplate = "/GUID={requestGUID}", ResponseFormat = WebMessageFormat.Json)]
public bool IsDataReady(string requestGUID)
{
    bool isReady = Global.publicDataDictionary.Keys.Contains(requestGUID);
    using (savitasEntities2 db = new savitasEntities2())
    {
        DataRequestLog drl = new DataRequestLog();
        drl.registrationID = "";
        drl.request = "Is Ready=" + isReady;
        drl.connectionID = "";
        drl.created = System.DateTime.Now.ToUniversalTime();
        drl.direction = "tickler";
        drl.dataRequestGUID = requestGUID;
        db.DataRequestLogs.Add(drl);
        db.SaveChanges();
    }
    return isReady;
}

我希望这肯定会奏效。现在,此方法将以JSON格式返回 isReady 。如果您未在 OperationContract 中指定 ResponseFormat ,则默认情况下它将返回 XML

I hope that this will definitely work. Now this method will return isReady in JSON format. If you do not specify ResponseFormat in your OperationContract, then by default it will be returned into XML.

现在尝试删除 Content-Type:application / json; charset = utf-8 因为您没有以 JSON 发送数据并执行您的方法。响应将采用JSON格式,因此,您必须解析它以获得 isReady 值。

Now try removing Content-Type: application/json; charset=utf-8 as you are not sending data in JSON and execute your method. Response will be in JSON format, so, you have to parse it to get the isReady value.

使用此呼叫服务的URL:

Use this URL to call service:

url: "http://www.blah.com/services/TestsService.svc/GUID=abc123"

这很简单,易于实现。感谢您的耐心等待。

This is simple and easily implementable. Thanks for your patience.

编辑:添加 BodyStyle = WebMessageBodyStyle.Bare OperatcionContract

这篇关于语法错误:jQuery $ .ajax调用中的字符无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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