当前位置:网站首页>1-21 jsonp interface

1-21 jsonp interface

2022-06-30 21:29:00 Endless pie

review JSONP The concept and characteristics of

Concept : Browser pass

characteristic

  1. JSONP It doesn't belong to the real Ajax request , Because it's not used XMLHttpRequest This object .
  2. JSONP Support only GET request , I won't support it POST、PUT、DELETE Equal request .

establish JSONP Interface precautions
If... Has been configured in the project CORS Cross-domain resource sharing , In order to prevent conflict , Must be configured CORS The middleware previously declared JSONP The interface of . otherwise JSONP The interface will be processed as open CORS The interface of .

Realization JSONP Interface steps

  1. Get the name of the callback function sent by the client
  2. Get through JSONP Data sent to the client as
  3. According to the data from the first two parts , Splice a string of function calls .
  4. Concatenate the string obtained in the previous step , Response to
// Must be configured cors Previous configuration JSONP
app.get('/api/jsonp',(req,res)=>{
// Get the name of the function 
const funcname = req.query.callback
 const data = {
    name:'zs',
    age:22
}
const scriptStr = `${funcname}(${JSON.stringify(data)})`

res.send(scriptStr)
})
原网站

版权声明
本文为[Endless pie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206302125367907.html