附加表单值到jQuery Ajax POST

Append form values to jQuery Ajax POST

本文关键字:jQuery Ajax POST 表单      更新时间:2023-09-26

我试图通过AJAX发布一个从值到远程API。

我需要从HTML表单元素的author, string, false, true

现在我已经硬核值但是

function sendData() {
  var settings = {
          "async": true,
          "crossDomain": true,
          "timeout":8000,
          "url": "http://localhost:8984/solr/techproducts/schema",
          "method": "POST",
          "headers": {
            "content-type": "application/json",
            "cache-control": "no-cache",
            "Access-Control-Allow-Origin":"*"
          },
          "processData": false,
          "data": "{'"replace-field'":{'"name'":'"author'",'"type'":'"string'",'"stored'":false,'"indexed'":true} }"
        }
        $.ajax(settings).done(function (response) {
          console.log(response);
        });
}

试试

var requestData = {
    id : $('#id').val(),
    commentLiveStatusStageChange:$('#'+textid).val(),
    currentLiveStatusStage:$('#stage').val()
}
 var settings = {
          "async": true,
          "crossDomain": true,
          "timeout":8000,
          "url": "http://localhost:8984/solr/techproducts/schema",
          "method": "POST",
          "headers": {
            "content-type": "application/json",
            "cache-control": "no-cache",
            "Access-Control-Allow-Origin":"*"
          },
          "processData": false,
          "data": requestData 
        }

您可以序列化表单数据,然后添加它。

交货:formData = $("#myForm").serialize()

在ajax请求中使用formdata

function sendData() {
  formData = $("#myForm").serialize() //myForm should be replaced with your form's id
  var settings = {
          "async": true,
          "crossDomain": true,
          "timeout":8000,
          "url": "http://localhost:8984/solr/techproducts/schema",
          "method": "POST",
          "headers": {
            "content-type": "application/json",
            "cache-control": "no-cache",
            "Access-Control-Allow-Origin":"*"
          },
          "processData": false,
          "data": formData
        }
        $.ajax(settings).done(function (response) {
          console.log(response);
        });
}

您可以使用$( "form" ).serialize();这样的东西来发布您的表单值:

  $.post( "http://localhost:8984/solr/techproducts/schema", $( "#testform" ).serialize() );