Javascript 使用jquery autocomplete,我试图获取文本框中的值,因为在文本字段中,数据不是来自使用ajax的json
这是我第一次将ajax与json一起用于自动完成(Jquery),结果看起来像自动完成,但结构(输出)看起来像表 这是我的jquery代码Javascript 使用jquery autocomplete,我试图获取文本框中的值,因为在文本字段中,数据不是来自使用ajax的json,javascript,jquery,json,ajax,autocomplete,Javascript,Jquery,Json,Ajax,Autocomplete,这是我第一次将ajax与json一起用于自动完成(Jquery),结果看起来像自动完成,但结构(输出)看起来像表 这是我的jquery代码 $("document").ready(function (){ $(function () { $.ajax({ url: "dummy.json", success: function(){ alert("got the file"); }, e
$("document").ready(function (){
$(function () {
$.ajax({
url: "dummy.json",
success: function(){
alert("got the file");
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
}
});
return false;
});
});
这是我的json数据
{
"schoolname":{
"school":[
{
"id":"1",
"description":"COL000001",
"schoolname":"Emirates College of Technology- UAE"
},
{
"id":"2",
"description":"COL000002",
"schoolname":"Al Khawarizmi International College- UAE"
},
{
"id":"3",
"description":"COL000003",
"schoolname":"Syscoms College"
},
{
"id":"4",
"description":"Other",
"schoolname":"Other 1"
}
]
}
}
我在firefox的console.log(xhr.status)中遇到一个错误;200和控制台日志(thrownError);无效的XML:
我是新来的我弄不明白为什么这不起作用我试着把console.log放进错误函数
这是我的html代码,我的自动完成
<input type="text" class="ipt_Field" id="scl_name">
提前谢谢
Mahadevan如果AJAX调用将数据解释为XML而不是JSON,则可能会出现问题。要解决这个问题,请尝试为AJAX调用指定数据类型。例如:
$.ajax({
url: "dummy.json",
dataType: "json",
success: function() {
alert("got the file");
},
error:function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
也可以考虑使用<强> < /强>
编辑:contentType用于将数据发送到服务器。从服务器检索数据时使用数据类型。如果AJAX调用将数据解释为XML而不是JSON,则可能会出现问题。要解决这个问题,请尝试为AJAX调用指定数据类型。例如:
$.ajax({
url: "dummy.json",
dataType: "json",
success: function() {
alert("got the file");
},
error:function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
也可以考虑使用<强> < /强>
编辑:contentType用于将数据发送到服务器。并且,从服务器检索数据时使用数据类型。很可能您的服务器正在使用错误的MIME类型提供json文件。将
数据类型:“json”
添加到ajax配置中可能会解决这个问题。如果没有,您需要将服务器配置为正确提供json。很可能您的服务器正在使用错误的MIME类型提供json文件。将数据类型:“json”
添加到ajax配置中可能会解决这个问题。否则,您需要将服务器配置为正确提供json。这是不正确的contentType
是发送到服务器的类型dataType
是返回的类型。@SexyTurnip-ahh是!你说得对。我正在修正我的答案,这是不正确的contentType
是发送到服务器的类型dataType
是返回的类型。@SexyTurnip-ahh是!你说得对。我正在修正我的答案。