如何通过jquery ajax调用在php页面上获取值

How to get value on php page by jquery ajax call

本文关键字:获取 php 何通过 jquery ajax 调用      更新时间:2023-09-26

我使用jquery ajax脚本获取php页面的内容而不加载页面。使用以下脚本

$('#data_subpartition_result').live('click', function(){
    $.ajax({
           type: "POST",
           url: "temp/model_view_result.php",
           context: "language=php&version=5",
           data: {train: 0.7, validation: 0.2, test: 0.1},
           success: function(result){
                    $('div.divRightModelContent').html(result);
               }
    });
});

和我的PHP文件代码如下

<?php 
     echo $_POST['train'];
     // some other php stuff
?>

我正在尝试打印或使用我通过jquery ajax调用的post方法传递的数据值,但我有一个错误,没有通过jquery ajax调用在PHP页面上的post方法传递任何值。告诉我怎么得到这个值。

我用PHP编程已经有好几年了,但这里有一些jQuery的事情需要纠正。

  1. context没有按您使用的方式使用。它用于告诉$.ajax调用加载url属性的特定部分。
  2. 因为你指定了type: "POST",所以你的.html()调用应该是:('div.divRightModelContent').html(result.d)
在官方的jQuery.ajax()网页上有一些很好的例子。它们至少在每个属性下提供了示例数据。