简体   繁体   English

由ajax发布到php时未定义的数据?

[英]undefined data when posting to php by ajax?

this is my javascript code 这是我的javascript代码

    var ActivityType ='d';
    var TotalActivities = 2;
    function marker(ActivityType,TotalActivities)

    {

    var dataTosend='typ='+ActivityType+'&total='+TotalActivities;

    $.ajax({

    url: 'activity.php',

    type: 'POST',

    data:dataTosend,

    async: true,

    success: function (data) {

    alert(data)

    },

    });

    }
marker();

this is my activity.php file 这是我的activity.php文件

<?php
echo $_POST['typ'];
echo $_POST['total'];

?>

when i call marker(); 当我打电话给marker(); in js i got undefined undefined in alert js我在alert得到undefined undefined

why does it says undefined data ? 为什么会显示未定义的数据?

but there is no error means typ ,& total parameter are reaching there 但没有错误意味着typ ,& total参数已到达

but why it says undefined 但是为什么它说未定义

You aren't passing in the variables as parameters when you call marker() . 调用marker()时,您不会将变量作为参数传递。

The arguments in the function are same name as the outer variables so inside the function scope, the argument versions are undefined and the outer variables are shadowed by the same name arguments 函数中的参数与外部变量的名称相同,因此在函数范围内,参数的版本未定义,外部变量被相同名称的参数遮盖

Try calling: 尝试致电:

marker(ActivityType,TotalActivities);

When you have call marker function the arguments are missing. 当您具有调用标记功能时,参数将丢失。

marker(ActivityType,TotalActivities);

you can also use this format when you have send the data using ajax. 使用ajax发送数据时,也可以使用这种格式。

    data:{typ:ActivityType,total:TotalActivities}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM