将表单值附加到jQuery Ajax POST

时间:2015-10-15 07:25:41

标签: javascript jquery html ajax

我正在尝试通过AJAX将值从一个值发布到远程API。

我需要HTML表单元素中的authorstringfalsetrue

现在我已经硬着价值了

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

3 个答案:

答案 0 :(得分:0)

试试这个

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 
        }

答案 1 :(得分:0)

你可以序列化formdata然后追加它。

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

答案 2 :(得分:0)

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

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