簡體   English   中英

訪問Jquery / AJAX發送的$ _POST數據

[英]Accessing $_POST data sent by Jquery/AJAX

我正在使用這個功能:

function end_incident() {
    var dataString = 'name=Daniel&phone=01234123456';
    $.ajax({
        type: "POST",
        url: "http://www.example.co.uk/erc/end_incident.php",
        data: dataString,
        success: function(msg){ 
            alert('Success!'+dataString);
        }
    });
};

將信息發送到end_incident.php ,但我無法訪問$_POST變量。 我試過這樣做:

$name = $_POST['name'];
$phone = $_POST['phone'];

難道我做錯了什么?

謝謝你的幫助

嘗試將數據作為對象發送:

function end_incident() {
    $.ajax({
       type: "POST",
       url: "http://www.example.co.uk/erc/end_incident.php",
       data: { name: "Daniel", phone: "01234123456" },
       success: function(msg){ 
            alert('Success!');
       }
    });
};

確保您請求的網址與您網站的同一來源相同 ,如果不是,您就會遇到跨網站腳本問題。 只有這樣:

  • 在瀏覽器中獲得“更高”的訪問/權限,即創建附加/擴展,或使用Greasemonkey
  • 通過您自己的站點使用代理來獲取文件請求:

     var getURL = "http://www.example.co.uk/erc/end_incident.php"; $.ajax({ type: "POST", url: "/get_url.php?url=" + encodeURIComponent(getURL), data: { name: "Daniel", phone: "01234123456" }, success: function(msg){ alert('Success!'); } }); 

我建議你為你的ajax添加一個error函數。 令人驚訝的是,有多少人專注於success而從不處理錯誤!

error: function()
{
   console.log(arguments);
}

暫無
暫無

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

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