gpt4 book ai didi

javascript - 使用jquery自动完成我试图获取文本框中的值作为表数据不是来自在文本字段中使用ajax的json

转载 作者:行者123 更新时间:2023-11-28 18:52:35 24 4
gpt4 key购买 nike

这是我第一次使用 ajax 和 json 进行自动完成(Jquery),结果看起来像自动完成,但结构(输出)看起来像表格。

这是我的 jquery 代码

$("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 和 console.log(thrownError);无效的 XML:

我是新的,我很困惑为什么这不起作用我尝试将 console.log 放入错误函数中

这是我的 html 代码,我的自动完成

<input type="text" class="ipt_Field" id="scl_name">

提前致谢马哈德万

最佳答案

如果 AJAX 调用将您的数据解释为 XML 而不是 JSON,则可能会出现此问题。要解决此问题,请尝试为 AJAX 调用指定dataType。例如:

$.ajax({
url: "dummy.json",
dataType: "json",
success: function() {
alert("got the file");
},
error:function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});

此外,请考虑使用 http://api.jquery.com/jquery.getjson/

编辑:将数据发送到服务器时使用contentType。并且,从服务器检索数据时使用dataType

关于javascript - 使用jquery自动完成我试图获取文本框中的值作为表数据不是来自在文本字段中使用ajax的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com