Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过AJAX/JS发送更多数据_Javascript_Ajax - Fatal编程技术网

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

Javascript 如何通过AJAX/JS发送更多数据,javascript,ajax,Javascript,Ajax,在下面的代码片段中,Ajax将向response.php发送一些数据。如果我想通过Ajax发送更多数据,应该如何处理?我想添加例如user_id,如底部的HTML代码所示 $(document).ready(function() { $("#FormSubmit").click(function (e) { e.preventDefault(); if($("#contentText").val()==='') {

在下面的代码片段中,Ajax将向response.php发送一些数据。如果我想通过Ajax发送更多数据,应该如何处理?我想添加例如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

<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>

弗尔格
使用


您可以手动生成postData,如:

var myData ={

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

user_id:$('#user_id').val()
}
但是jquery提供了一种自动生成表单数据的方法

var str = $("formabc").serialize();
您需要使用id=“formabc”将所有要发送到服务器的元素包装成一个表单

将您的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>

弗尔格

您不必使用JSON。您所要做的就是用有效负载创建一个字符串(在您的示例中附加到myData)并将其发送到您的服务器。

那么我应该如何实现它呢?
<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>