jQuery 如何通过 Ajax 发送多个数据字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6085649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send multiple data fields via Ajax?
提问by deadconversations
I'm stuck: I'm trying to submit a form using AJAX, but I can't find a way to send multiple data fields via my AJAX call.
我被卡住了:我正在尝试使用 AJAX 提交表单,但我找不到通过 AJAX 调用发送多个数据字段的方法。
$(document).ready(function() {
$("#btnSubmit").click(function() {
var status = $("#activitymessage").val();
var name = "Ronny";
$.ajax({
type: "POST",
url: "ajax/activity_save.php",
**data: "status="+status+"name="+name"**,
success: function(msg) {...
I've tried all sorts of stuff:
我尝试了各种各样的东西:
data: {status: status, name: name},
Or even stuff like this just for testing purposes:
甚至像这样的东西只是为了测试目的:
data: "status=testing&name=ronny",
But whatever I try, I get nothing in my activity_save.php
thus nothing in my SQL.
但是无论我尝试activity_save.php
什么,我的 SQL 中都没有得到任何结果。
So, what's the correct syntax to put more lines of data in my AJAX call?
那么,在我的 AJAX 调用中放入更多行数据的正确语法是什么?
回答by Avitus
The correct syntax is:
正确的语法是:
data: {status: status, name: name},
As specified here: http://api.jquery.com/jQuery.ajax/
如此处指定:http: //api.jquery.com/jQuery.ajax/
So if that doesn't work, I would alert those variables to make sure they have values.
因此,如果这不起作用,我会提醒这些变量以确保它们具有值。
回答by k-dev
You can send data through JSON or via normal POST, here is an example for JSON.
您可以通过 JSON 或通过普通 POST 发送数据,这里是 JSON 的示例。
var value1 = 1;
var value2 = 2;
var value3 = 3;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "yoururlhere",
data: { data1: value1, data2: value2, data3: value3 },
success: function (result) {
// do something here
}
});
If you want to use it via normal post try this
如果你想通过普通帖子使用它试试这个
$.ajax({
type: "POST",
url: $('form').attr("action"),
data: $('#form0').serialize(),
success: function (result) {
// do something here
}
});
回答by Alberthoven
Try with quotes:
尝试使用引号:
data: {"status": status, "name": name}
It must work fine.
它必须工作正常。
回答by Shahin
var countries = new Array();
countries[0] = 'ga';
countries[1] = 'cd';
after that you can do like:
之后你可以这样做:
var new_countries = countries.join(',')
after:
后:
$.ajax({
type: "POST",
url: "Concessions.aspx/GetConcessions",
data: new_countries,
...
This thing work as JSON string format.
这东西作为 JSON 字符串格式工作。
回答by TheGreenGentleman
This one works for me.
这个对我有用。
Here's my PHP:
这是我的PHP:
<div id="pageContent">
<?php
while($row = mysqli_fetch_assoc($stmt)) {
?>
<br/>
<input id="vendorName_" name="vendorName_<?php echo $row["id"]; ?>" value='<?php echo $row["vendorName"]; ?>'>
<input id="owner_" name="owner_<?php echo $row["id"]; ?>" value='<?php echo $row["owner"]; ?>'>
<input id="city_" name="city_<?php echo $row["id"]; ?>" value='<?php echo $row["city"]; ?>'>
<button id="btn_update_<?php echo $row["id"]; ?>">Update</button>
<button id="btn_delete_<?php echo $row["id"]; ?>">Delete</button>
<?php
}
?>
</br></br>
<input id = "vendorName_new" value="">
<input id = "owner_new" value="">
<input id = "city_new" value="">
<button id = "addNewVendor" type="submit">+ New Vendor</button>
</div>
Here's my jQuery using AJAX:
这是我使用 AJAX 的 jQuery:
$("#addNewVendor").click(function() {
alert();
$.ajax({
type: "POST",
url: "create.php",
data: {vendorName: $("#vendorName_new").val(), owner: $("#owner_new").val(), city: $("#city_new").val()},
success: function(){
$(this).hide();
$('div.success').fadeIn();
showUsers()
}
});
});
回答by Amir
According to http://api.jquery.com/jquery.ajax/
根据http://api.jquery.com/jquery.ajax/
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
回答by Shan
I am a beginner at ajax but I think to use this "data: {status: status, name: name}" method datatype must be set to JSON i.e
我是 ajax 的初学者,但我认为使用这种“数据:{状态:状态,名称:名称}”方法数据类型必须设置为 JSON 即
$.ajax({
type: "POST",
dataType: "json",
url: "ajax/activity_save.php",
data: {status: status, name: name},
回答by user8161624
Try to use :
尝试使用:
$.ajax({
type: "GET",
url: "something.php",
data: { "b": data1, "c": data2 },
dataType: "html",
beforeSend: function() {},
error: function() {
alert("Error");
},
success: function(data) {
$("#result").empty();
$("#result").append(data);
}
});
回答by Kanin Peanviriyakulkit
Use this
用这个
data: '{"username":"' + username + '"}',
I try a lot of syntax to work with laravel it work for me for laravel 4.2 + ajax.
我尝试了很多语法来使用 laravel,它适用于 laravel 4.2 + ajax。
回答by user4184048
Try this:
尝试这个:
$(document).ready(function() {
$("#btnSubmit").click(function() {
var status = $("#activitymessage").val();
var name = "Ronny";
$.ajax({
type: "POST",
url: "ajax/activity_save.php",
data: {'status': status, 'name': name},
success: function(msg) {...