[英]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!');
}
});
};
確保您請求的網址與您網站的同一來源相同 ,如果不是,您就會遇到跨網站腳本問題。 只有這樣:
通過您自己的站點使用代理來獲取文件請求:
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.