在typeahead.js源代码上传递ajax回调



我正在使用jQuery 2.1.4,并试图修改Twitter typeahead.js 0.11.1以接受ajax请求作为源。当我从本地文件系统file:///C:/...运行页面时,它按预期工作,返回错误函数并显示建议noData,但当我尝试直接从服务器甚至jsfiddle运行页面时会返回ajax成功或错误函数,但建议不会出现。回调似乎不起作用。

您认为这是与回调有关还是ajax请求使用不当?

以下是我的代码:

HTML

<input class="typeahead" type="text" placeholder="Names" id="tah1">

Javascript

$('.typeahead').typeahead({
    hint: true,
    highlight: true,
    minLength: 1
},
{
    name: 'userNames',
    source: 
        function findMatches(q, callback) {
            console.log('q: ' + q);
            var soapEnv =
                "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> "
                    + "<soap:Body>"
                        + "<ResolvePrincipals xmlns='http://schemas.microsoft.com/sharepoint/soap/'>"
                            + "<principalKeys>"
                            + "<string>" + q + "</string>"
                            + "</principalKeys>"
                            + "<principalType>All</principalType>"
                            + "<addToUserInfoList>true</addToUserInfoList>"
                        + "</ResolvePrincipals>"
                    + "</soap:Body>"
                + "</soap:Envelope>"    
            $.ajax({
                url: "www./_vti_bin/People.asmx?op=ResolvePrincipals",
                type: "POST",
                dataType: soapEnv,
                success: function(data){
                        console.log('ajax request successful');
                        callback(data);
                    },
                error: function(data){
                        console.log('ajax request error');
                        var errorValues = ['noData'];
                        callback(errorValues);
                    },
                contentType: "text/xml; charset="utf-8"",
                SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/ResolvePrincipals"
            });
        }
});

我还创建了这个jsfiddle。

提前感谢!

我认为这是关于跨域请求:

 $.ajax({
    crossDomain: true,
    url: "www./_vti_bin/People.asmx?op=ResolvePrincipals",
    type: "POST",
    dataType: soapEnv,
    success: function(data){
        console.log('ajax request successful');
        callback(data);
    },
    error: function(data){
        console.log('ajax request error');
        var errorValues = ['noData'];
        callback(errorValues);
    },
    contentType: "text/xml; charset="utf-8"",
    SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/ResolvePrincipals"
});

使用选项"crossDomain"并将其设置为true。不要在JSFiddle上工作,但无法访问您的url。

最新更新