关于javascript–如何将参数传递给ExpresspostHTTP方法?和js传递参数给servlet的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于java–如何将参数传递给Res
关于javascript – 如何将参数传递给Express post HTTP方法?和js传递参数给servlet的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于java – 如何将参数传递给Rest-Assured、javascript – Angular 2.将参数传递给组件、javascript – Angular拖放 – 如何将参数传递给onStart回调函数、javascript – express 4.x将http重定向到https等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- javascript – 如何将参数传递给Express post HTTP方法?(js传递参数给servlet)
- java – 如何将参数传递给Rest-Assured
- javascript – Angular 2.将参数传递给组件
- javascript – Angular拖放 – 如何将参数传递给onStart回调函数
- javascript – express 4.x将http重定向到https
javascript – 如何将参数传递给Express post HTTP方法?(js传递参数给servlet)
我正在构建一个简单的REST API(使用PouchDB和Vue.js).现在,我可以用几个字段创建项目:
server.js:
var express = require(''express'')
var PouchDB = require(''pouchdb'')
var app = express()
var db = new PouchDB(''vuedb'')
app.post(''/projects/new'', function(req, res) {
var data = {
''type'': ''project'',
''title'': '''',
''content'': '''',
''createdAt'': new Date().toJSON()
}
db.post(data).then(function (result) {
// handle result
})
})
client.js:
// HTML
<input type="text"v-model="title" placeholder="Enter title">
<input type="text"v-model="content" placeholder="Enter content">
<buttonv-on:click="submit">Submit</button>
// JS
submit () {
this.$http.post(''http://localhost:8080/projects/new'').then(response => {
// handle response
})
}
如何传递参数来设置标题和内容?在REST API中执行此操作的常规方法是什么?
解决方法:
在服务器端,您可以使用req.body访问客户端在POST请求中发送的数据.
所以你的server.js文件是这样的:
var express = require(''express'')
var PouchDB = require(''pouchdb'')
var app = express()
var db = new PouchDB(''vuedb'')
app.post(''/projects/new'', function(req, res) {
var data = {
''type'': ''project'',
''title'': req.body.title,
''content'': req.body.content,
''createdAt'': new Date().toJSON()
}
db.post(data).then(function (result) {
// handle result
})
})
在客户端,您必须使用对象作为$http.post的第二个参数传递POST请求的主体. client.js看起来像这样:
// HTML
<input type="text"v-model="title" placeholder="Enter title">
<input type="text"v-model="content" placeholder="Enter content">
<buttonv-on:click="submit">Submit</button>
// JS
submit () {
this.$http.post(''http://localhost:8080/projects/new'', {
title: ''Your title'',
content: ''The content''
}).then(response => {
// handle response
})
}
总结
以上是小编为你收集整理的javascript – 如何将参数传递给Express post HTTP方法?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
原文地址:https://codeday.me/bug/20190623/1270200.html
java – 如何将参数传递给Rest-Assured
当我调用这项服务时,http://restcountries.eu/rest/v1/,我得到了几个国家的信息.
但是当我想获得像芬兰这样的特定国家信息时,我会调用网络服务http://restcountries.eu/rest/v1/name/Finland来获取与国家相关的信息.
要自动执行上述方案,如何在Rest-Assured中参数化国家/地区名称?我在下面试过,但没有帮助我.
RestAssured.given(). parameters("name","Finland"). when(). get("http://restcountries.eu/rest/v1/"). then(). body("capital",containsstring("Helsinki"));
解决方法
REST Assured will automatically try to determine which parameter type
(i.e. query or form parameter) based on the HTTP method. In case of
GET query parameters will automatically be used and in case of POST
form parameters will be used.
但在你的情况下,似乎你需要路径参数而不是查询参数.
另请注意,获取国家/地区的通用URL是http://restcountries.eu/rest/v1/name/{country}
其中{country}是国家/地区名称.
然后,还有多种方式来传输路径参数.
这里有几个例子
使用pathparam()的示例:
// Here the key name 'country' must match the url parameter {country} RestAssured.given() .pathParam("country","Finland") .when() .get("http://restcountries.eu/rest/v1/name/{country}") .then() .body("capital",containsstring("Helsinki"));
使用变量的示例:
String cty = "Finland"; // Here the name of the variable have no relation with the URL parameter {country} RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/name/{country}",cty) .then() .body("capital",containsstring("Helsinki"));
现在,如果您需要调用不同的服务,您还可以像这样参数化“服务”:
// Search by name String val = "Finland"; String svc = "name"; RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",svc,val) .then() .body("capital",containsstring("Helsinki")); // Search by ISO code (alpha) val = "CH" svc = "alpha" RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",containsstring("Bern")); // Search by phone intl code (callingcode) val = "359" svc = "callingcode" RestAssured.given() .when() .get("http://restcountries.eu/rest/v1/{service}/{value}",containsstring("Sofia"));
之后您还可以轻松使用JUnit @RunWith(Parameterized.class)为单元测试提供参数’svc’和’value’.
javascript – Angular 2.将参数传递给组件
<top [mode]="tree">Loading...</top>
在我的组件中,我包括来自angular2 / core的输入
import {Input,Component,OnInit} from 'angular2/core';
在我的组件类中,我声明了一个输入
@input() mode: string;
并且使用console.log()我尝试捕获’tree’的传递参数,但它未定义.
console.log(this,this.mode);
组件文件的完整代码:
import {Http,HTTP_PROVIDERS} from 'angular2/http'; import {Input,OnInit} from 'angular2/core'; import {ParticipantService} from '../services/participant.service'; import {orderBy} from '../pipes/orderby.pipe'; @Component({ selector: 'top',templateUrl: 'dev/templates/top.html',pipes: [orderBy],providers: [HTTP_PROVIDERS,ParticipantService] }) export class AppTopComponent implements OnInit { constructor (private _participantService: ParticipantService) {} errorMessage: string; participants: any[]; @input() mode: string; ngOnInit() { console.log(this,this.mode); this.getParticipants('top3'); var self = this; setInterval(function() { self.getParticipants('top3'); },3000); } getParticipants(public mode: string) { this._participantService.getParticipants(mode) .then( participants => this.participants = participants,error => this.errorMessage = <any>error ); } }
解决方法
因此树必须是父组件中存在的对应于字符串的东西.
如果要使用字符串树,请使用以下命令:
<top mode="tree">Loading...</top>
您可以注意到这些参数不能用于根组件.有关详细信息,请参阅此问题:
> Angular 2 input parameters on root directive
javascript – Angular拖放 – 如何将参数传递给onStart回调函数
我有一个使用角度drag and drop的应用程序.
除了一件事,一切都很好.
我需要在onStart回调函数中传递一个参数,但我不知道如何.
我四处搜寻并尝试了几种可能性但没有成功.
函数本身正在工作,已被调用并正确执行,我唯一的问题是将参数传递给它.
代码:
在这个例子中,我做了一个尝试.
<div> <divdata-drop="true" ng-model='todo_list' jqyoui-droppable="{multiple:true,onDrop:'update_item()'}"> <div> <divng-repeat="item in todo_list track by $index" ng-show="item.title" data-drag="true" data-jqyoui-options="{revert: 'invalid'}" ng-model="todo_list" jqyoui-draggable="{index: {{$index}},onStart:'set_board_item_id_panel(event,ui,{board_item_id: item.board_item_id})'}">{{item.title}}</div> </div> </div> </div>
问题:
如何在角度drag and drop的回调函数中传递参数?
非常感谢你!
解决方法
替换……
onStart:'set_board_item_id_panel(event,{board_item_id: item.board_item_id})'
随…
onStart:'set_board_item_id_panel({board_item_id: item.board_item_id})'
然后在您的Controller中执行此操作…
.... $scope.set_board_item_id_panel = function (event,board_item_id) { console.log(board_item_id); } ....
javascript – express 4.x将http重定向到https
var https = require('https'); var http = require('http'); var express = require('express'); var app = express(); var router = express.Router(); app.use('/',router); //listen server on https var server = https.createServer(config.sslCredential,app); server.listen(config.serverPort); //listen server on http,and always redirect to https var httpServer = http.createServer(function(req,res){ res.redirect(config.serverDomain+req.url); }); httpServer.listen(config.httpServerPort);
但不知何故,我无法将https请求重定向到https请求,我应该如何使用express 4.x在node.js上正确执行此操作?
解决方法
app.all('*',ensureSecure); // at top of routing calls http.createServer(app).listen(80) https.createServer(sslOptions,app).listen(443) function ensureSecure(req,res,next){ if(req.secure){ // OK,continue return next(); }; // handle port numbers if you need non defaults // res.redirect('https://' + req.host + req.url); // express 3.x res.redirect('https://' + req.hostname + req.url); // express 4.x }
今天关于javascript – 如何将参数传递给Express post HTTP方法?和js传递参数给servlet的介绍到此结束,谢谢您的阅读,有关java – 如何将参数传递给Rest-Assured、javascript – Angular 2.将参数传递给组件、javascript – Angular拖放 – 如何将参数传递给onStart回调函数、javascript – express 4.x将http重定向到https等更多相关知识的信息可以在本站进行查询。
本文标签: