如何使用jquery ajax将数据传递到另一个页面

问题描述:

我在ajax调用时遇到问题。

I have a problem on ajax call.

这是关于ajax的代码:

Here is my code regarding the ajax:

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: 'studentNumber='+$('#StudentID').val(),
        success: function(data)
        {
            $('#curriculum').html(data);
        }
    });
});

当我在另一页上回显 studentNumber 时, studentNumber undefined 。为什么?

When I echo studentNumber on another page, the studentNumber is undefined. Why is that?

只需修改你的代码:

JS

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: { studentNumber: $('#StudentID').val() },
        success: function(data)
        {
            $('#curriculum').html(data);
        }
    });
});

PHP

<?php

    $var = $_POST['studentNumber'];

?>

如果你仍然无法使它工作..其他你应该考虑的事情..

If you still can not make it works.. other things you should consider..

url: '../portal/curriculum.php',

1)请使用完整的URL http://yourdomain.com/portal/curriculum.php 或绝对路径,如 /portal/curriculum.php

1) Please use full URL http://yourdomain.com/portal/curriculum.php or absolute path like /portal/curriculum.php

2)添加错误回调以查看错误消息

2) Add an error callback to check out the error message

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: { studentNumber: $('#StudentID').val() },
        success: function(data)
        {
            $('#curriculum').html(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});