Javascript能获取到GET的数据吗

Javascript能获取到GET的数据吗,第1张

可以,可以使用自定义函数

var $_GET = void function () {

    var url = windowdocumentlocationhreftoString();

    var u = urlsplit("");

    if (typeof(u[1]) === "string") {

        u = u[1]split("&");

        var get = {};

        for (var i in u) {

            var j = u[i]split("=");

            get[j[0]] = j[1];

        }

        return get;

    } else {

        return {};

    }

};

然后使用的时候这样子做:

var a = $_GET['a'] // a=1

当然需要,$_GET取得的数据是来自URL后的参数,这个是可以自己在地址栏里随便写的,而$_POST的数据来自表单提交,虽然在前端可以设置JS验证数据合法性,但是别有用心的人可以很容易地绕过这个JS验证脚本,所以,在后台接收到这些数据后,首先要做的,就是用过滤或者验证函数进行进一步地处理

js获取get数据

如今前后端分离已经成为趋势,也就是说许多页面如果外部做链接 就需要 自定义 话的 页面进行参数设置,目前有效的办法就是get参数。通过自定义的get参数为封装的 vue等框架 提供页面变量标识! 当然也可以使用路由进行。

/ --------------------实现2(返回 $_GET 对象, 仿PHP模式)---------------------- /

一、用file_get_contents以get方式获取内容,需要输入内容为:

1、<php

2、$url='>

3、$html=file_get_contents($url);

4、echo$html;

5、>

二、用file_get_contents函数,以post方式获取url,需要输入内容为

1、<php

2、$url='>

3、$data=array('foo'=>'bar');

4、$data=>

5、$opts=array(

6、'>

7、 'method'=>'POST',

8、 'header'=>"Content-type:application/x->

9、          "Content-Length:"strlen($data)"\r\n",

10、 'content'=>$data

11、)

12、);

13、$ctx=stream_context_create($opts);

14、$html=@file_get_contents($url,'',$ctx);

15、>

三、用fopen打开url,以get方式获取内容,需要输入内容为

1、<php

2、$fp=fopen($url,'r');

3、$header=stream_get_meta_data($fp);//获取信息

4、while(!feof($fp)){

5、$result=fgets($fp,1024);

6、}

7、echo"urlheader:{$header}<br>":

8、echo"urlbody:$result";

9、fclose($fp);

10、>

四、用fopen打开url,以post方式获取内容,需要输入内容为

1、<php

2、$data=array('foo2'=>'bar2','foo3'=>'bar3');

3、$data=>

4、$opts=array(

5、'>

6、'method'=>'POST',

7、'header'=>"Content-type:application/x->

8、"Content-Length:"strlen($data)"\r\n",

9、'content'=>$data

10、)

11、);

12、$context=stream_context_create($opts);

13、$html=fopen('>

14、$w=fread($html,1024);

15、echo$w;

16、>

五、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为

1、php

2、functionget_url($url,$cookie=false)

3、{

4、$url=parse_url($url);

5、$query=$url[path]""$url[query];

6、echo"Query:"$query;

7、$fp=fsockopen($url[host],$url[port]$url[port]:80,$errno,$errstr,30);

8、if(!$fp){

9、returnfalse;

10、}else{

11、$request="GET$query>

12、$request="Host:$url[host]\r\n";

13、$request="Connection:Close\r\n";

14、if($cookie)$request="Cookie:  $cookie\n";

15、$request="\r\n";

16、fwrite($fp,$request);

17、while(!@feof($fp)){

18、$result=@fgets($fp,1024);

19、}

20、fclose($fp);

21、return$result;

22、}

23、}

24、//获取url的html部分,去掉header

25、functionGetUrlHTML($url,$cookie=false)

26、{

27、$rowdata=get_url($url,$cookie);

28、if($rowdata)

29、{

30、$body=stristr($rowdata,"\r\n\r\n");

31、$body=substr($body,4,strlen($body));

32、return$body;

33、}

34、 returnfalse;

35、}

36、>

参考资料:

php-file_get_contents

以上就是关于Javascript能获取到GET的数据吗全部的内容,包括:Javascript能获取到GET的数据吗、php中,通过$_GET 和 $_POST 取得的数据 安不安全 需要什么过滤函数处理吗、js获取get数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9433167.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存