如何将javascript变量发送或传递给php文件?

时间:2017-04-11 10:04:40

标签: javascript php web-services rest

我有一个包含数组myfile.js的javascript文件。 我没有权利更改此js文件中的任何内容。

所以我添加了一个我通过Web服务调用的php文件,我将这个php文件称为POST请求,并以电子邮件作为参数。

所以我想要做的是在myfile.js中获取数组并将其传递给php文件,以便我可以检查该数组中是否存在该电子邮件,然后我可以作为json响应返回true或假的。

经过多次研究后,我发现了一种将js数组传递给php文件的方法,这是我的代码:

 <script type='text/javascript' src='js/content/myfile.js'></script>
    <script>
        myarray  = myfile.myarray;// this works ,i can get the array 

         var hashes = window.location.href.valueOf().split('?');
        // alert(hashes[1]);
        try {
         hashes = hashes[1].split('&');
            // alert(hashes[1]);
        hashes = hashes[1].split('=');

        }
        catch ( e){
            alert(e.message);
        }
         if (hashes[1] == 0) 
        {
            hashes[1]++;
            //alert(hashes);

        top.location.href='http://direct.chancepure.com/voyance/PpSpamtram.php?array='+myarray+'&count='+hashes[1];
        }
    </script>
    <?php 
    $phpArray=$_GET["array"];
/* then i check for the email existance then return a response ......*/

这段代码的作用是使用GET将js文件中的myarray添加到url:重定向到相同的url但是将数组添加到参数中, 注意:我添加了一个计数参数,以避免无限循环。

此方法在浏览器上运行正常,因为javascript代码和重定向有效,但是当我从Postman或其他工具调用它时。 需要你的帮助。

0 个答案:

没有答案