jQuery的自动完成(jQuery用户界面1.8rc3)与ASP.NET Web服务 [英] jQuery AutoComplete (jQuery UI 1.8rc3) with ASP.NET web service

查看:84
本文介绍了jQuery的自动完成(jQuery用户界面1.8rc3)与ASP.NET Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有这个版本的自动完成控制从ashx的处理程序返回XML时工作。该XML看起来是这样的:

Currently, I have this version of the autocomplete control working when returning XML from a .ashx handler. The xml looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<States>
<State>
  <Code>CA</Code> 
  <Name>California</Name> 
</State>
<State>
  <Code>NC</Code> 
  <Name>North Carolina</Name> 
</State>
<State>
  <Code>SC</Code> 
  <Name>South Carolina</Name> 
</State>

自动完成code是这样的:

The autocomplete code looks like this:

    $('.autocompleteTest').autocomplete(
    {
        source: function(request, response) {
            var list = [];
            $.ajax({
                url: "http://commonservices.qa.kirkland.com/StateLookup.ashx",
                dataType: "xml",
                async: false,
                data: request,
                success: function(xmlResponse) {
                    list = $("State", xmlResponse).map(function() {
                        return {
                            value: $("Code", this).text(),
                            label: $("Name", this).text()
                        };
                    }).get();
                }
            });
            response(list);
        },
        focus: function(event, ui) {
            $('.autocompleteTest').val(ui.item.label);
            return false;
        },
        select: function(event, ui) {
            $('.autocompleteTest').val(ui.item.label);
            $('.autocompleteValue').val(ui.item.value);
            return false;
        }

    });

由于种种原因,我宁愿被调用一个ASP.NET Web服务,但我不能得到它的工作。要通过更改为服务(我在做一个本地服务来保持简单),自动完成code的开端是:

For various reasons, I'd rather be calling an ASP.NET web service, but I can't get it to work. To change over to the service (I'm doing a local service to keep it simple), the start of the autocomplete code is:

    $('.autocompleteTest').autocomplete(
    {
        source: function(request, response) {
            var list = [];
            $.ajax({
                url: "/Services/GeneralLookup.asmx/StateList",
                dataType: "xml",

这code是一个页面上的网站的根和GeneralLookup.asmx是一个子文件夹命名的服务。但在Web服务的断点永远不会被击中,并且不产生自动完成列表。如果它的确与众不同,这来自于ASMX的XML是:

This code is on a page at the root of the site and the GeneralLookup.asmx is in a subfolder named Services. But a breakpoint in the web service never gets hit, and no autocomplete list is generated. In case it makes a difference, the XML that comes from the asmx is:

<?xml version="1.0" encoding="utf-8" ?> 
<string xmlns="http://www.kirkland.com/"><State> <Code>CA</Code> <Name>California</Name> </State> <State> <Code>NC</Code> <Name>North Carolina</Name> </State> <State> <Code>SC</Code> <Name>South Carolina</Name> </State></string>

其功能相当于,因为我从来没有使用在映射code盘根节点的名称。我还没有看到在大约从该控件调用的.asmx服务jQuery的文档任何东西,但阿贾克斯呼叫是阿贾克斯电话,对吧?

Functionally equivalent since I never use the name of the root node in the mapping code. I haven't seen anything in the jQuery docs about calling a .asmx service from this control, but a .ajax call is a .ajax call, right?

我试过了的.asmx(〜/服务/)各种不同的路径,我甚至已移动的服务是相同的路径,以消除这些问题。没有运气无论是。

I've tried various different paths to the .asmx (~/Services/), and I've even moved the service to be in the same path to eliminate these issues. No luck with either.

任何想法?

推荐答案

我自动完成通过使用JSON与的.asmx工作。这里是什么,我做了一个例子:

I got the autocomplete to work with .asmx by using JSON. Here is an example of what I did:

JavaScript的:

JavaScript:

$("#tbNameFilter").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Services/AutocompleteService.asmx/Aoi_Autocomplete",
            data: "{ 'q': '" + request.term + "', 'limit': '10' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function (data) { return data; },
            success: function (data) {
                response($.map(data.d, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            }
        });
    },
    minLength: 1
});

Web服务:

[WebMethod]
public List<FAD_Aoi> Aoi_Autocomplete(String q, int limit)

这篇关于jQuery的自动完成(jQuery用户界面1.8rc3)与ASP.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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