自动提示,WS和JSON问题 [英] autosuggest, ws and json problem

查看:102
本文介绍了自动提示,WS和JSON问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $。阿贾克斯({
        键入:POST,
        网址:/webservices/AutoSuggestWebService.asmx/GetSuggestedRestaurants
        数据:{'prefixText':'+ $('#ctl00_ctl00_cplMPBody_txtSearch)。VAL()+'},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON
        成功:函数(MSG){
            数据= msg.d;
            警报(msg.d)
            $(#ctl00_ctl00_cplMPBody_txtSearch)自动完成(数据)。
        }
    })

,其中数据是

  [一些文本,另一些textz,呵呵只是文本]

WS:

  [的WebMethod]
        公共字符串GetSuggestedRestaurants(对象prefixText)
        {
            JS的JavaScriptSerializer =新的JavaScriptSerializer();
            JSON字符串= js.Serialize(新RestaurantSearchHelper(DaoFactory).GetSearchSuggest(prefixText.ToString(),PageBase.CurrentCountry));
        }

但是,如果我通过文字搜索,所以我没有得到任何东西。

如果WS返回

  [的WebMethod]
        公共字符串GetSuggestedRestaurants(对象prefixText)
        {
            回归飞机dkhashd苹果救护车边境作物海水淡化大象鹦鹉;
        }

和JS看起来像

 数据= msg.d.split();
警报(数据)

然后数据看起来像

 飞机,dkhashd,苹果,救护车,边框,作物,海水淡化,大象,鹦鹉

和自动提示工作。如果数据是什么,是拨错与第一JSON

  [一些文本,另一些textz,呵呵只是文本]


解决方案

您第一个错误是文本

  {'prefixText:喇嘛喇嘛'}

不会有效的JSON字符串。这可能是正确的对象初始化,但它是错误的EN codeD JSON对应 http://www.json.org/ 并的 RFC4627 。正确的JSON字符串将是

  {prefixText:喇嘛喇嘛}

或以下

 数据:{prefixText:'+ $('#ctl00_ctl00_cplMPBody_txtSearch)VAL()+'}'

在你的情况(有一些小的可能出现的问题,如果 $('#ctl00_ctl00_cplMPBody_txtSearch)。VAL()有或内部\\字符)。我建议你<一个HREF =htt​​p://www.jsonlint.com/相对=nofollow> http://www.jsonlint.com/ 网站,您可以验证任何JSON数据。

接下来的问题是在服务器端。该ASMX Web方法应该像下面的

  [的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公开名单&LT;串GT; GetSuggestedRestaurants(字符串prefixText){
    返回新RestaurantSearchHelper(DaoFactory).GetSearchSuggest(
                   prefixText,
                   PageBase.CurrentCountry);
}

和相应的类应该具有 [ScriptService] 属性。

我建议你使用 JSON.stringify 方法:

 数据:JSON.stringify({prefixText:$('#ctl00_ctl00_cplMPBody_txtSearch)VAL()})

代替人工JSON序列化。看到我的<一个细节href=\"http://stackoverflow.com/questions/5119977/jquery-ajax-response-always-returns-nothing/5127142#5127142\">other回答其中包括链接到工作演示项目。

$.ajax({
        type: "POST",
        url: "/webservices/AutoSuggestWebService.asmx/GetSuggestedRestaurants",
        data: "{'prefixText': '" + $('#ctl00_ctl00_cplMPBody_txtSearch').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            data = msg.d;
            alert(msg.d)
            $("#ctl00_ctl00_cplMPBody_txtSearch").autocomplete(data);
        }
    })

where data is

["some text","another some textz","huh just text"]

WS:

[WebMethod]
        public string GetSuggestedRestaurants(object prefixText)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(new RestaurantSearchHelper(DaoFactory).GetSearchSuggest(prefixText.ToString(), PageBase.CurrentCountry));
        }

but if i search by word "so" i don't get anything.

If ws return

[WebMethod]
        public string GetSuggestedRestaurants(object prefixText)
        {
            return  "Aeroplane dkhashd Apple Ambulance Border Crops Desalination Elephants  Parrot ";
        }

and js looks like

data = msg.d.split(" ");
alert(data)

then data looks like

Aeroplane,dkhashd,Apple,Ambulance,Border,Crops,Desalination,Elephants,Parrot

and autosuggest work. What is worng with first json if data is

["some text","another some textz","huh just text"]

解决方案

Your first error is that the text

{'prefixText': 'bla bla'}

is NOT valid JSON string. It could be correct object initializer in JavaScript, but it is wrong encoded JSON corresponds to http://www.json.org/ and RFC4627. Correct JSON string will be

{"prefixText": "bla bla"}

or the following

data: '{"prefixText": "' + $('#ctl00_ctl00_cplMPBody_txtSearch').val() + '"}'

in your case (with some small possible problems if $('#ctl00_ctl00_cplMPBody_txtSearch').val() has " or \ characters inside). I recommend you http://www.jsonlint.com/ site where you can validate any JSON data.

The next problem is on the server side. The ASMX web method should be like the following

[WebMethod]
[ScriptMethod (ResponseFormat = ResponseFormat.Json)]
public List<string> GetSuggestedRestaurants(string prefixText) {
    return new RestaurantSearchHelper(DaoFactory).GetSearchSuggest(
                   prefixText,
                   PageBase.CurrentCountry);
}

and the corresponding class should has [ScriptService] attribute.

I recommend you to use JSON.stringify method:

data: JSON.stringify({ prefixText: $('#ctl00_ctl00_cplMPBody_txtSearch').val() })

instead of manual JSON serialization. See details in my other answer which include link to the working demo project.

这篇关于自动提示,WS和JSON问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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