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 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"