在jqueryUI自动完成中使用JSON-Object

时间:2022-12-01 18:13:25

I'm loading a JSON array from an external Domain. I have gotten this to work correctly yet I am still having problems actually showing any of the results. Using examples and other question/answers I have gotten as far as an attempt of displaying the results apparently being made, but ending in an uncaught error:

我正在从外部域加载JSON数组。我已经使这个工作正确,但我仍然有问题显示任何结果。使用例子和其他问题/答案,我已经尝试显示结果,显然是做了,但以一个未被发现的错误结束:

Uncaught TypeError: Cannot read property 'label' of undefined

未捕获的类型错误:无法读取未定义的属性'label'

Code snippet:

代码片段:

$('#citySearchBox').autocomplete({
      source: function(request, response){
          $.ajax({
              url: "/api.php",
              data: {action: "search", input: request.term},
              dataType: "jsonp"
          });
          response(function(data){
              return $.parseJSON(data);
          });
      },
      minLength: 3
  });

Server Response as seen in debugger:

调试器中的服务器响应:

[{"id":"1.3.0.0.0.0","label":"Berlin (10115)","value":"index.php?action=city&city=1.3.0.0.0.0"}, etc...]

[{ " id ":" 1.3.0.0.0.0”、“标签”:“柏林(10115)”,“价值”:“index . php ?行动= city&city = 1.3.0.0.0.0”},等等……)

As you can see I have attempted to parse the string into a JS Object, with shown method and also tried JSON.parse(data).

正如您所看到的,我尝试用show method将字符串解析为JS对象,还尝试了JSON.parse(data)。

If I've made a mistake earlier than the "response" part but somehow managed to half-way bodge it please point out my mistakes.

如果我在“回复”部分之前犯了一个错误,但不知何故,我做了个折中,请指出我的错误。

EDIT:

编辑:

I've changed the code after Bradys suggestion, but I seem to be doing something wrong with the syntax, as putting a break-point inside of the response function nothing is stopped.

在Bradys的建议之后,我修改了代码,但是我似乎在语法上做错了什么,因为在响应函数中放置了断点,没有任何东西被停止。

This is the updated response function:

这是更新后的响应功能:

response( function(data){
        var items = data.text().slice(1, -1).split(',');
        for(i=0; i < (items.length - 3); i=i+3)
        {
            items[i] = $.parseJSON(items[i]) + ','
             + $.parseJSON(items[i+1]) + ',' + $.parseJSON(items[i+2]);
        }
        return items;
    });

Case closed, never actually got JSONP to work, but the situation changed and was no longer reliant on cross-domain communication.

Case closed,从来没有让JSONP工作,但是情况改变了,不再依赖跨域通信。

For anybody interested working code is as follows:

对于任何有兴趣的工作代码如下:

  $('#searchBox').autocomplete({
      source: function(request, response){
          $.ajax({
              type: "GET",
              url: "api.php",
              async: true,
              data: {action: "search", input: request.term},
              success: function(data){
                  var items  = $.parseJSON(data);
                  response(items);
              }
          });
      },
      select: function(event, ui){
          $('#searchBox').val(ui.item.value);
          window.location.href = ui.item.url;
      },
      minLength: 2
  });

1 个解决方案

#1


1  

What you're getting from the server is not a JSON string. It's an array. You should split up the array and parse each object individually.

从服务器得到的不是JSON字符串。这是一个数组。应该对数组进行拆分并分别解析每个对象。

#1


1  

What you're getting from the server is not a JSON string. It's an array. You should split up the array and parse each object individually.

从服务器得到的不是JSON字符串。这是一个数组。应该对数组进行拆分并分别解析每个对象。