简体   繁体   English

jQuery Ajax发布不起作用

[英]jQuery Ajax post is not working

I know that there is a lot of questions like this out there, but I have been surfing them and other website for like 4 hours trying to figure this out. 我知道这里有很多类似的问题,但是我花了4个小时在网上冲浪他们和其他网站,以期弄清楚这一点。 I am trying to get main.js to post the data via ajax and then the php should echo that data. 我试图让main.js通过ajax发布数据,然后php应该回显该数据。 If it does not work, it will echo "null". 如果不起作用,它将回显“空”。 It keeps echoing "null" instead of "John". 它一直在回显“ null”而不是“ John”。 I know that the jquery and main.js links work, I have tested them. 我知道jquery和main.js链接有效,我已经对其进行了测试。 Here is main.js: 这是main.js:

$(document).ready(function(){
    $.post("index.php", { test: "John"} );
});

And here is the php part of index.php: 这是index.php的php部分:

<?php 
    $var = "null";
    if(isset($_POST['test'])) { 
    $var = $_POST['test'];
    }
    echo $var;
?>

I hope you can solve my problem, and thank you in advance. 希望您能解决我的问题,并先谢谢您。

You are missing the callback function with the response from the server. 您缺少服务器响应的回调函数。

$.post( "index.php",{ test: "John"}, function( data ) {
  alert(data);
});

Or you can do something like this: 或者,您可以执行以下操作:

 $.post( "index.php",{ test: "John"})
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

Please check the documentation Documentation 请检查文档文档

Give this a shot 试一下

jQuery jQuery的

var available agent = 1;
jQuery.ajax({
    type: "POST",
    url: "your-url",
    data: available_agent,
    dataType: 'json',
    cache: false,
    contentType: false,
    processData: false,
    success:  function(data){
        owner = data['round_robin_agent'];
    },
    error : function () {
       alert("error");
    }
});

PHP Script PHP脚本

public function round_robin() {
    //Do your work
    $round_robin_agent = 'rob';
    $results_array = array(
        'round_robin_agent' => $round_robin_agent
    );
    return json_encode($results_array);
}

Download HTTP Trace chrome extension, traceback ajax call and share a screenshot. 下载HTTP Trace chrome扩展程序,traceback ajax调用并共享屏幕截图。

https://chrome.google.com/webstore/detail/http-trace/idladlllljmbcnfninpljlkaoklggknp https://chrome.google.com/webstore/detail/http-trace/idladlllljmbcnfninpljlkaoklggknp

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

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