简体   繁体   English

Drupal:如何使用AJAX POST从REST服务提交Web

[英]Drupal: How to submit the webfrom from REST services using AJAX POST

I am having web form module in drupal. 我在drupal中有Web表单模块。 I want to submit a form into webform from external app with REST service using the AJAX POST method. 我想使用AJAX POST方法从具有REST服务的外部应用程序向Webform提交表单。 I can able to retrieve the web form (node/1) using ajax. 我可以使用ajax检索Web表单(node / 1)。 On submitting it should not create as new node, instead it will get add into web form. 提交时,不应将其创建为新节点,而应将其添加到Web表单中。

Thanks in advance for any help..... 预先感谢您的任何帮助.....

To be able to retrieve data from and send data to Webform module with an external application I suggest you to use Webform Service module of Drupal that makes REST API available for your surveys. 为了能够使用外部应用程序从Webform模块检索数据并将数据发送到Webform模块 ,我建议您使用Drupal的Webform Service模块,该模块使REST API可用于您的调查。

Once you've installed and configured the modules and its dependencies (if any) you'll be able to retrieve the serialized data of a survey. 一旦安装并配置了模块及其依赖项(如果有),便可以检索调查的序列化数据。 Also you can send submission by an appropriate request to the endpoint with proper HTTP method (GET, POST etc..). 您也可以使用适当的HTTP方法(GET,POST等)通过适当的请求将提交发送到端点。 See description of endpoints on the page of Webform Service module. 请参阅Webform Service模块页面端点描述。

You need to know the format of the data you are going to submit. 您需要知道要提交的数据格式。 Hereby you will find the right format of JSON object. 因此,您将找到正确的JSON对象格式。 The value of the values key is an array either it has one or more values, so most of the time it looks like this: values键的values是一个数组,它具有一个或多个值,因此大部分时间看起来像这样:

{'values':{0:"put your value here"}}

In case of multiple values (checkbox, multi-select) it is possible to use values like this: 如果有多个值(复选框,多选),则可以使用以下values

{'values':{
    0:"put your value1 here",
    1: "put your value2 here",
}}

Finally here is a snippet of HTML and AngularJS to prepare and post a request to the REST API. 最后,这里是HTML和AngularJS的代码片段,用于准备请求并将其发布到REST API。

HTML HTML

<form ng-submit="submit('put the UUID of the survey here')" ng-controller="formCtrl">
  <label>Your name:</label><br/>
  <input type="text" ng-model="formData[1]['values'][0]" /><br/>
  <label>I like tea</label><br/>
  <input type="checkbox" ng-model="formData[2]['values'][0]" value="tea" /><br/>
  <label>I like coffee</label><br/>
  <input type="checkbox" ng-model="formData[2]['values'][1]" value="coffee" /><br/>
  <input type="submit" value="Send" />
</form>

Note that you don't need to write static HTML form, but you can retrieve the data of the survey from the API and build the form by a script. 请注意,您无需编写静态HTML表单,但可以从API检索调查的数据并通过脚本构建表单。

JavaScript JavaScript的

var app = angular.module('webformApp', []);

app.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

app.controller('formCtrl', function($scope, $element, formService) {
    $scope.submit = function(uuid){
      formService.postForm(uuid, this);
    };
});

app.service('formService', function($http){
    return {
        postForm: function(uuid, $scope){
          var toPost = {
            webform: uuid,
            submission: {
                data: $scope.formData
            }
          };
          console.log(toPost);
          console.log(JSON.stringify(toPost));
          return $http.post('http://example.com/yourendpoint/submission', toPost);
        }
    };
});

To try/test the API itself you can use Postman extension of Chrome browser. 要尝试/测试API本身,可以使用Chrome浏览器的Postman扩展。 This JS will log the JSON string to the console to send it to API. 该JS将JSON字符串记录到控制台以将其发送到API。

Have fun! 玩得开心!

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

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