如何使用JQuery Ajax将数据发送到PHP文件?
问题描述:
我正在尝试使用jQuery和Ajax将数据发送到php文件.但是,我只能以json格式接收来自ajax的响应,并且无法发送数据.
I am trying to send data to php file using jQuery and ajax. However, I am only able to receive the response from ajax in json format and unable to send data.
$.ajax({
url: 'myFile.php',
type: 'GET',
data: {ID:1},
dataType:'json',
cache: false,
beforeSend:function(e){},
processData: '',
success: function(response){
Initialize(response);
},
error: function(err){alert('error')}
});
答
您已经做好了一切.
$.ajax({
url: 'myFile.php',
type: 'GET',
data: {
ID: 1,
Test: 'Anothervalue'
},
dataType: 'json',
cache: false,
beforeSend: function(e) {},
processData: '',
success: function(response) {
Initialize(response);
},
error: function(err) {
alert('error')
}
});
使用php可以通过
$_GET['ID']
$_GET['Test']
您也可以通过使用url来发送数据
You can send data by concating with url also
myFile.php?ID=1&Test=Anothervalue
但是如果要使用POST
发送数据或要发送大量数据,则始终可以选择data: {}
格式进行发送,因为它更干净.
But if want send data with POST
or have much data to send you can always choose data: {}
format to send because it's cleaner.
data: {
ID: 1,
Test: 'Anothervalue',
// More
}