简体   繁体   English

将表单值附加到jQuery Ajax POST

[英]Append form values to jQuery Ajax POST

I'm trying to post a from values to a remote API via AJAX. 我正在尝试通过AJAX将a从值发布到远程API。

I need the author , string , false , true from the HTML form element. 我需要HTML表单元素中的authorstringfalsetrue

Right now I have hardcored to values bu 现在,我已经坚守价值观bu

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

try like this 这样尝试

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 
        }

you can serialize the formdata and then append it. 您可以序列化formdata,然后附加它。

Ex: formData = $("#myForm").serialize() 例如: formData = $("#myForm").serialize()

Then use formdata in ajax request 然后在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);
        });
}

You can use $( "form" ).serialize(); 您可以使用$( "form" ).serialize(); Something like this to post your form values: 这样的东西来发布您的表单值:

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

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

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