douqiang4245 2012-06-21 20:47
浏览 66
已采纳

使用json和php进行jquery自动完成

I'm trying to duplicate this, http://jqueryui.com/demos/autocomplete/#remote-jsonp. Only, instead of querying the geonames.org server I'd like to query my own. Trouble is that tutorial doesn't mention anything about what geonames is doing on their end.

I tried to wing it and grab $_POST variables but I guess I don't understand what the ajax is passing. $_POST['name_startsWith'] doesn't exist which is what seems to be being passed. Can anyone enlighten me as to what the sent information looks like?

Also, the tutorial is regarding jsonp, not json. How important is that?

  • 写回答

6条回答 默认 最新

  • dongyi3616 2012-06-21 20:57
    关注

    JSONP and quite different to JSON. As others say JSONP is cross server for one.

    JSON is a better method for getting data form your own server and is much easier to translate and get working across all browsers.

    What is returned is a item array under the relative format of:

    [{label: '', description: ''}, { //etc }]
    

    The label is used as the display name and unless you bring extra data back like an ID it will be the only data used to select items in the auto complete.

    Using functions such as select you can take the ui.item.id (the id would be coming back with your array as defined above) and use that to select unique items from a list with which to perform further AJAX functions on.

    Edit:

    To use pass-backed vars to change the display of the menu you will need to write your own displayMenuItem function to utilise the vars from the array defined above.

    Fruther edit:

    The way they actually do this is two sided. The remote server just passes back an echo'ed JSON format array, like the one defined above and nothing else (well except for headers). After that JQuery just sorts thje array out and fills the data in.

    So an example in PHP of doing this:

    echo json_encode(array('name' => 'whoop'));
    

    And literally that's it on the remote server side...

    Edit again I forgot the callback sent wit JSONP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀