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

时间:2021-04-13 20:36:58

This is the first time I am using ajax with json for autocomplete (Jquery) the result will look like the auto-complete but the structure(output) will look like table.

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

Here is my jquery code

这是我的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;
   });
});

Here is my json data

这是我的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"
     }
  ]
 }
}

I am getting an error in firefox for console.log(xhr.status); 200 and console.log(thrownError); Invalid XML:

我在firefox中为console.log(xhr.status)收到错误; 200和console.log(thrownError);无效的XML:

I am new this I confused why this was not working I tried putting console.log in error function

我很新,我很困惑为什么这不起作用我试着把console.log放在错误函数中

This is my html code where my autocomplete

这是我的自动填充功能的html代码

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

Thanks in advance Mahadevan

在此先感谢Mahadevan

1 个解决方案

#1


2  

The problem can occur if the AJAX call interprets your data as XML rather than JSON. To solve this, try specifying the dataType for your AJAX call. For example:

如果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);
    }
});

Also, consider using http://api.jquery.com/jquery.getjson/.

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

EDIT: contentType is used when data is sent to the server. And, dataType is used when data is retrieved from the server.

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

#1


2  

The problem can occur if the AJAX call interprets your data as XML rather than JSON. To solve this, try specifying the dataType for your AJAX call. For example:

如果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);
    }
});

Also, consider using http://api.jquery.com/jquery.getjson/.

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

EDIT: contentType is used when data is sent to the server. And, dataType is used when data is retrieved from the server.

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