I'm using jQuery 2.1.4 and trying to modify Twitter typeahead.js 0.11.1 to accept an ajax request as a source.
When I run the page from the local file system file:///C:/..., it works as expected, the error function is returned and the suggestion noData appears, but when I try to run the page directly from the server or even jsfiddle, the ajax success or error function are returned but the suggestions don't appear. The callback doesn´t seem to be working.
Do you think it is something related to the callback or the ajax request is being used improperly?
Below is my code:
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"
});
}
});
I also created this jsfiddle.
Thanks in advance!
1 个答案:
答案 0 :(得分:0)
I think it's about cross domain request:
$.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"
});
Use the options "crossDomain" and set it at true. Don't work on JSFiddle but your url is not reachable.