douhulao7642 2010-11-03 02:57
浏览 93
已采纳

从URL(如php的$ _GET)获取jQuery / Ajax的数据?

What I'm wanting to do is take a URL like myurl.com/page.php?data1=x&data2=y&data3=z and get each piece of data and use it in a jQuery function. Is there any built in way to do that?

To clarify, when I navigate to (say, pasting the url into the address bar, or going to it via a bookmark) I want jQuery to do something with that data.

As a bonus, if it also includes something to check if the data is set to something, that would be great.

  • 写回答

3条回答 默认 最新

  • dongxingji3882 2010-11-03 03:02
    关注

    A quick way to do it would be if your javascript code is embedded in a php file (not an external link to a .js file), you could just do a PHP print right in between your <script></script> tags. It would work with bookmarks and pasting the URL to the address bar.

    For example:

    <script type="text/javascript">
    var get_var;
    get_var = <? print $_GET['varname']; ?>;
    // some jQuery code here
    </script>
    

    This should only be used as a proof of concept. This is vulnerable to XSS attacks, and should not be used in production. This list of XSS prevention techniques is a good place to start (but outside the scope of the OP's question).

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看