Express.js – res.jsonp() 方法
res.jsonp() 方法发送了带有 JSONP 支持的 json 响应。此方法类似于 res.json() 方法,唯一的区别在于它提供了 JSONP 回调支持。
语法
res.jsonp ( [body] )
例子 1
使用 res.jsonp() 方法:
// res.jsonp([body]) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
res.jsonp({ title: 'Welcome to Tutorials Point !' });
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});输出
C:\home
ode>> node resJsonp.js Server listening on PORT 3000
使用 GET 请求,访问以下终结点:https://:3000/api
{"title":"Welcome to Tutorials Point !"}例子 2
我们来看另一个例子。
// res.jsonp([body]) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
// With the next() middleware
app.use('/api', function(req, res, next) {
res.jsonp({ name: 'Parent Method' });
next();
})
app.get('/api', function(req, res) {
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});输出
C:\home
ode>> node resJsonp.js Server listening on PORT 3000
使用 GET 请求访问以下终结点 – https://:3000/api
{"name":"Parent Method"}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP