Jquery工具栏帮助:访问外部php脚本


Jquery toolbar help: accessing external php script

嗨,我现在正在做一个工具栏,遇到了一个问题。我有两个文件,工具栏和PHP脚本。PHP脚本只是从数据库中提取一个标题数组。我想做的是让工具栏调用这个脚本并返回这个数组,然后使用这个数组创建一个下拉菜单,标题作为选项。

我假设我需要以某种形式使用Ajax,但不知道如何实现这一点。

许多谢谢。

更新:由于相同的站点策略,我在跨浏览器获得。getjson工作时遇到了问题。问题是javascript文件的调用产生一个工具栏,这是附加到远程站点,这个工具栏应该调用php脚本回到我自己的服务器上从数据库获取数据。

如何解决这个问题?

是。如果你想让PHP和Javascript一起工作,答案是:JSON。您必须在php中对数组进行JSON编码。使用Javascript Ajax调用PHP文件,然后将响应parseJSON。然后在Javascript中使用数组。之后,你可以在Javascript中渲染它,并将结果附加到你想要的元素。

在jQuery文档中有很多例子。如果您不熟悉ajax,请阅读文档:http://api.jquery.com/jQuery.ajax/最后有一些简单的示例。你也可以使用。getjson。
jQuery.getJSON( url, [data,] [success(data, textStatus, jqXHR)] )    
- url A string containing the URL to which the request is sent.    
- data A map or string that is sent to the server with the request.    
- success(data, textStatus, jqXHR) A callback function that is executed if the request succeeds.
基本上

:

$.getJSON("url of the php file",
          data to send to the php file if any,
          function(data) {
            console.log(data);
          }
});

在这个函数里面是console.log,你可以对你的数据做任何你想做的事情。