使用Ajax将Javascript变量提交到另一个PHP文件

时间:2022-09-15 09:26:59
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready( function () { //The page has been loaded...
    var ref = document.referrer; //Referrer
    var curpg = document.location.href; //Current page URL
    var dataSet = 'ref=' + ref + '&curpg=' curpg + '&shizz1e=21';

    $.ajax({
        type: "GET",
        url: "http://www.domainname.com/ajaxreceiver.php",
        data: dataSet
    });
    return false;
});
</script>

I am trying to submit the 'curpg', 'ref', and 'shizz1e' variables to ajaxreceiver.php as soon as the page loads, without any user intervention. I am also trying to avoid having the page refresh or redirect the client to ajaxreceiver.php. I'm not sure why this code isn't working; the dataSet variable never gets sent to ajaxreceiver.php.

我试图在页面加载后立即将'curpg','ref'和'shizz1e'变量提交给ajaxreceiver.php,而无需任何用户干预。我也试图避免页面刷新或将客户端重定向到ajaxreceiver.php。我不确定为什么这段代码不起作用; dataSet变量永远不会被发送到ajaxreceiver.php。

3 个解决方案

#1


0  

I would have to see your server-side PHP code because your JavaScript code looks OK to me. I think it should work.

我必须看到你的服务器端PHP代码,因为你的JavaScript代码看起来不错。我认为它应该工作。

Try this rewriting it like this, though:

试试这样重写它,但是:

<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript">
    $(document).ready( function () {
        var referrer = document.referrer;
        var current_page_url = $(location).attr('href');
        var data_object = {
            'ref':        referrer,
            'curpg':      current_page_url,
            'shizz1e':    '21'
        };

        $.ajax({
            'url':        "http://your.url.com/php_script.php",
            'data':       data_object,
            'success':    function (return_data) {
                // Do something on success.
            }
        });
    });
</script>

This is just a bit cleaner in my opinion. Please show some of your server-side PHP code, so I can really help you find out what's going wrong.

在我看来,这只是一点点清洁。请显示一些服务器端的PHP代码,这样我就可以帮助您找出问题所在。

#2


0  

If you're going to be sending a URL as a parameter you're going to have to escape certain characters within it, using the encodeURI() or encodeURIComponent() functions.

如果您要将URL作为参数发送,则必须使用encodeURI()或encodeURIComponent()函数来转义其中的某些字符。

#3


0  

1) make sure, your request gets actually sent - check your networktab and add those handlers to have some debug-help:

1)确保您的请求实际发送 - 检查您的网络选项卡并添加这些处理程序以获得一些调试帮助:

$.ajax({/*...*/})
   .done(function()  { console.log("success"); })
   .fail(function(jqXHR, status)  { console.log("error: "+status); })
   .always(function(){ console.log("complete"); });

2) Did you accidently do a forbidden cross-domain call? http://www.domainname.com and http://domainname.com is a common example causing this issue. Use a realtive path instead: /ajaxreceiver.php is enough.

2)您是否意外地进行了禁止的跨域呼叫? http://www.domainname.com和http://domainname.com是导致此问题的常见示例。请改用一个实际路径:/ajaxreceiver.php就足够了。

3) provide your php code, so that we can check for errors in there. Perhaps you accidently used [POST] or some tiny mistake like that.

3)提供您的PHP代码,以便我们可以检查那里的错误。也许你意外地使用了[POST]或类似的一些小错误。

#1


0  

I would have to see your server-side PHP code because your JavaScript code looks OK to me. I think it should work.

我必须看到你的服务器端PHP代码,因为你的JavaScript代码看起来不错。我认为它应该工作。

Try this rewriting it like this, though:

试试这样重写它,但是:

<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript">
    $(document).ready( function () {
        var referrer = document.referrer;
        var current_page_url = $(location).attr('href');
        var data_object = {
            'ref':        referrer,
            'curpg':      current_page_url,
            'shizz1e':    '21'
        };

        $.ajax({
            'url':        "http://your.url.com/php_script.php",
            'data':       data_object,
            'success':    function (return_data) {
                // Do something on success.
            }
        });
    });
</script>

This is just a bit cleaner in my opinion. Please show some of your server-side PHP code, so I can really help you find out what's going wrong.

在我看来,这只是一点点清洁。请显示一些服务器端的PHP代码,这样我就可以帮助您找出问题所在。

#2


0  

If you're going to be sending a URL as a parameter you're going to have to escape certain characters within it, using the encodeURI() or encodeURIComponent() functions.

如果您要将URL作为参数发送,则必须使用encodeURI()或encodeURIComponent()函数来转义其中的某些字符。

#3


0  

1) make sure, your request gets actually sent - check your networktab and add those handlers to have some debug-help:

1)确保您的请求实际发送 - 检查您的网络选项卡并添加这些处理程序以获得一些调试帮助:

$.ajax({/*...*/})
   .done(function()  { console.log("success"); })
   .fail(function(jqXHR, status)  { console.log("error: "+status); })
   .always(function(){ console.log("complete"); });

2) Did you accidently do a forbidden cross-domain call? http://www.domainname.com and http://domainname.com is a common example causing this issue. Use a realtive path instead: /ajaxreceiver.php is enough.

2)您是否意外地进行了禁止的跨域呼叫? http://www.domainname.com和http://domainname.com是导致此问题的常见示例。请改用一个实际路径:/ajaxreceiver.php就足够了。

3) provide your php code, so that we can check for errors in there. Perhaps you accidently used [POST] or some tiny mistake like that.

3)提供您的PHP代码,以便我们可以检查那里的错误。也许你意外地使用了[POST]或类似的一些小错误。