简体   繁体   中英

Append form values to jQuery Ajax POST

I'm trying to post a from values to a remote API via AJAX.

I need the author , string , false , true from the HTML form element.

Right now I have hardcored to values 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.

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

Then use formdata in ajax request

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(); Something like this to post your form values:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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