简体   繁体   English

如何通过AJAX / JS发送更多数据

[英]How to send more data through AJAX/JS

In my code snippet below my Ajax will send some data to response.php. 在我的Ajax下面的代码段中,会将一些数据发送到response.php。 How should i proceed if I want to send more data through Ajax? 如果要通过Ajax发送更多数据,该怎么办? I want to add for example user_id as shown in my HTML code at the bottom. 我想添加例如user_id,如底部的HTML代码所示。

$(document).ready(function() {

    $("#FormSubmit").click(function (e) {
            e.preventDefault();
            if($("#contentText").val()==='')
            {
                alert("Du følger allerede denne bloggen");
                return false;
            }
            var myData = 'content_txt='+ $("#contentText").val(); 
            jQuery.ajax({
            type: "POST", 
            url: "assets/plugin/ajax-follow/response.php", 
            dataType:"text", 
            data:myData, 
            success:function(response){
                $("#responds").append(response);
                $("#contentText").val(''); 
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
            });
    });

});

HTML 的HTML

<div class="form_style">
<input type="hidden" name="content_txt" id="contentText" value="'.$user_info[u_id].'">
<input type="hidden" name="user_id" id="userid" value="'.$whotofollow[u_id].'">
<button id="FormSubmit">Følg</button>
</div>

Use JSON 使用JSON

$(document).ready(function() {

$("#FormSubmit").click(function (e) {
        e.preventDefault();
        if($("#contentText").val()==='')
        {
            alert("Du følger allerede denne bloggen");
            return false;
        }
      var myData = 'content_txt='+ $("#contentText").val(); //build a post data structure
      var userId = $("#user_id").val();
      var JsonData = {userId : userId,myData : myData};       // Build a JSON object 

        jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "assets/plugin/ajax-follow/response.php", //Where to make Ajax calls
        dataType:"JSON", // Data type, HTML, json etc.
        data : JsonData, //Form variables
        success:function(response){
            $("#responds").append(response);
            $("#contentText").val(''); //empty text field on successful
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
        });
});

});

You can manual build postData like: 您可以手动构建postData,例如:

var myData ={

content_txt:$("#contentText").val(),

user_id:$('#user_id').val()
}

But jquery provide a method that automatic build form data for you! 但是jquery提供了一种自动为您构建表单数据的方法!

var str = $("formabc").serialize();

you need wrap all element you want send to server in a form with id="formabc" 您需要使用id =“ formabc”的形式包装要发送到服务器的所有元素

Change your html to: 将您的html更改为:

<div class="form_style">
<form id="formabc">
<input type="hidden" name="content_txt" id="contentText" value="'.$user_info[u_id].'">
<input type="hidden" name="user_id" id="userid" value="'.$whotofollow[u_id].'">
<button id="FormSubmit">Følg</button>
</form>
</div>

You don't have to use JSON. 不必使用JSON。 All you have to do is create a String (append to myData in your example) with the payload and send it to your server. 您要做的就是使用有效负载创建一个String(在示例中附加到myData)并将其发送到您的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM