使用Javascript将数据发布到处理页面

时间:2013-11-21 18:31:12

标签: javascript php jquery

我正在尝试将数据发布到后台处理php页面。在JavaScript运行之后,我运行了一些不应该成为问题的PHP。

这是我的代码:

<script type="text/javascript" src="jquery-1.8.2.min.js"></script>

<script>
$(function(){
       $.ajax({
            url: 'https://processingsiteurl.com/register2.php',
            type: 'POST', // GET or POST
            data: 'f_refer_by="129"&sinup_id="2"&sinup_z_First_Name_="Rossontf1"&sinup_z_Last_Name_="Ras4z123a"&sinup_z_Email_="6s11albertoramirez@armyangel.com"&sinup_z_Choose_Password_="abc123"&sinup_z_Confirm_Password_="abc123"&sinup_z_Fundraiser_Display_Name="Test123"&sinup_z_Fundraiser_Dates="Test123"&sinup_z_Members_in_Organization="73"&sinup_z_Email_Format_="1"&sinup_z_Name_of_Campaign="Test123".val()', // will be in $_POST on PHP side
            success: function(data) { // data is the response from your php script
                // This function is called if your AJAX query was successful
                alert("Response is: " + data);
            },
            error: function(data) {
                // This callback is called if your AJAX query has failed
                alert("Error!" + data);
            }
        });
    });
</script>

问题是我收到了“[object Object]”错误。然后剧本就死了。

2 个答案:

答案 0 :(得分:1)

简化你的ajax数据并将其放在一个对象中,而不是一个字符串;你自己的生活太过艰难。例如:

// Everything before data goes here
        data: {
            "f_refer_by": "129",
            "sinup_id": "2",
            "sinup_z_First_Name_": "Rossontf1",
            "sinup_z_Last_Name_": "Ras4z123a",
            "sinup_z_Email_": "6s11albertoramirez@armyangel.com",
            "sinup_z_Choose_Password_": "abc123",
            "sinup_z_Confirm_Password_": "abc123",
            "sinup_z_Fundraiser_Display_Name": "Test123",
            "sinup_z_Fundraiser_Dates": "Test123",
            "sinup_z_Members_in_Organization": "73",
            "sinup_z_Email_Format_": "1",
            "sinup_z_Name_of_Campaign": '"Test123".val()' // will be in $_POST on PHP side
        },
// Everything after data goes here

此外,您的值包含"Test123".val()。我怀疑这是你想要的。听起来你可能意味着像$('#Test123').val()这样的东西,但我不知道。

答案 1 :(得分:0)

你可以试试这个:

    var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","https://processingsiteurl.com/register2.php",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("f_refer_by=129&sinup_id=2.......other parameters.....");