簡體   English   中英

jQuery Ajax發布不起作用

[英]jQuery Ajax post is not working

我知道這里有很多類似的問題,但是我花了4個小時在網上沖浪他們和其他網站,以期弄清楚這一點。 我試圖讓main.js通過ajax發布數據,然后php應該回顯該數據。 如果不起作用,它將回顯“空”。 它一直在回顯“ null”而不是“ John”。 我知道jquery和main.js鏈接有效,我已經對其進行了測試。 這是main.js:

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

這是index.php的php部分:

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

希望您能解決我的問題,並先謝謝您。

您缺少服務器響應的回調函數。

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

或者,您可以執行以下操作:

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

請檢查文檔文檔

試一下

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腳本

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);
}

下載HTTP Trace chrome擴展程序,traceback ajax調用並共享屏幕截圖。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM