AJAX – 多个数据

我卡住了:我正在尝试使用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) {... 

我尝试了各种各样的东西:

 data: {status: status, name: name}, 

甚至像这样的东西只是为了testing的目的:

 data: "status=testing&name=ronny", 

但是,无论我尝试什么,我什么都没有在我的activity_save.php因此没有在我的SQL。

那么,在我的AJAX调用中放入更多行数据的正确语法是什么?

正确的语法是: http : //api.jquery.com/jQuery.ajax/

 data: {status: status, name: name}, 

如果这不起作用,我会提醒这些variables,以确保他们有价值。

你可以通过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 somthing here } }); 

如果你想通过普通的post使用它,试试这个

  $.ajax({ type: "POST", url: $('form').attr("action"), data: $('#form0').serialize(), success: function (result) { // do somthing here } }); 
 var countries = new Array(); countries[0] = 'ga'; countries[1] = 'cd'; 

之后,你可以这样做:

 var new_countries = countries.join(',') 

后:

 $.ajax({ type: "POST", url: "Concessions.aspx/GetConcessions", data: new_countries, ... 

这东西工作作为JSONstring格式。

尝试用引号:

 data: {"status": status, "name": name} 

它必须正常工作。

根据http://api.jquery.com/jquery.ajax/

 $.ajax({ method: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert( "Data Saved: " + msg ); }); 

我是ajax的初学者,但我认为使用这个“data:{status:status,name:name}”方法的数据types必须设置为JSON ie

 $.ajax({ type: "POST", dataType: "json", url: "ajax/activity_save.php", data: {status: status, name: name}, 

这个为我工作。

这是我的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> 

这是我使用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() } }); }); 

用这个

 data: '{"username":"' + username + '"}', 

我尝试了很多语法来与laravel一起工作,为laravel 4.2 + 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) {... 

我是新来的AJAX,我已经尝试了这一点,它运作良好。

 function q1mrks(country,m) { // alert("hellow"); if (country.length==0) { //alert("hellow"); document.getElementById("q1mrks").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("q1mrks").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","../location/cal_marks.php?q1mrks="+country+"&marks="+m,true); //mygetrequest.open("GET", "basicform.php?name="+namevalue+"&age="+agevalue, true) xmlhttp.send(); } 

只需添加“&用户”或使用json序列化将其变成json

头部划伤2天后,这是对我有用的东西。 为什么我不能得到AJaX的'数据'设置发送两个键/值(包括一个variables包含原始图像数据)是一个谜,但这似乎是jQuery.param()函数编写的;

用你的variables创build一个params数组,不加引号:

 var params = { key_name1: var_1, key_name2: var_2 }; // etc. var ser_data = jQuery.param( params ); // arbitrary variable name 

使用variablesser_data作为你的数据值;

  $.ajax({ type: 'POST', url: '../php_handler_url.php', data: ser_data, }).success(function(response) { alert(response); }); 

文档在这里: https : //api.jquery.com/jQuery.param/

希望有所帮助!

尝试使用:

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