node跨域請求方法小結(jié)
本文介紹了node跨域請求,主要介紹了兩種方法,一種是jsonp,另一種res.wirteHead,具體如下:
第一種:jsonp
參看用nodejs實現(xiàn)json和jsonp服務(wù)
第二種:res.wirteHead
node部分
var http = require('http') var url = require('url') var querystring = require('querystring') var port = 9000 var jsonData = { 'name': 'xiaohong', 'job': 'daboss' } http.createServer(function (req, res) { // var pathStr = url.parse(req.url) res.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8', 'Access-Control-Allow-Credentials': true, 'Access-Control-Allow-Origin': '*' }) var type = req.method; if (type == 'GET') { res.end(JSON.stringify(jsonData)) } else if (type == 'POST') { var str = ''; req.on('data',function(chunk){ str += chunk; }) req.on('end',function(){ var data = querystring.parse(str) console.log(data) if(data.name == "" || data.job == ""){ res.end(JSON.stringify({'success':true,msg:'填寫有誤'})) }else{ res.end(JSON.stringify({'success':false,msg:'添加成功'})) } }) } }).listen(port, function () { console.log('server is runing at port ' + port) })
重點部分是添加響應(yīng)頭信息
res.writeHead(200, { 'Content-Type': 'application/json;charset=utf-8', 'Access-Control-Allow-Credentials': true, 'Access-Control-Allow-Origin': '*' //可以是*,也可以是跨域的地址 })
在ajax
里不需要做任何特殊處理
dataType
仍舊是json
html部分
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <a class="click" href="javascript:get_jsonp()" rel="external nofollow" >click me</a> <p class="result"></p> <label>姓名:</label> <input class="name" type="text" /> <label>職位:</label> <input class="job" type="text"> <a class="add" href = "javascript:add()">添加</a> <p class="msg"></p> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> function get_jsonp() { $.ajax({ type: 'get', dataType: 'json', url: 'http://localhost:9000', success: function (data) { $('.result').html('my name is ' + data.name) }, error: function (err) { $('.result').html('出錯了 ' + err.status) } }) } function add(){ $.ajax({ type:'post', url:'http://localhost:9000', dataType:'json', data:{ 'name':$(".name").val(), 'job':$(".job").val() }, success:function(data){ $('.msg').html(data.msg) }, error:function(err){ $('.msg').html('出錯了'+err.status) } }) } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實戰(zhàn)node靜態(tài)文件服務(wù)器的示例代碼
本篇文章主要介紹了實戰(zhàn)node靜態(tài)文件服務(wù)器的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Kubernetes Node刪除鏡像的實現(xiàn)步驟
本文介紹了在Kubernetes集群中如何刪除節(jié)點上的鏡像,包括連接節(jié)點、查看鏡像列表、使用Docker命令刪除鏡像以及驗證刪除結(jié)果的步驟,感興趣的可以了解一下2024-09-09Node.js中使用計時器定時執(zhí)行函數(shù)詳解
這篇文章主要介紹了Node.js中使用計時器定時執(zhí)行函數(shù)詳解,本文使用了Node.js中的setTimeout和setInterval函數(shù),需要的朋友可以參考下2014-08-08express框架,報錯:“Cannot set headers after t
這篇文章主要介紹了express框架,報錯:“Cannot set headers after they are sent to the client”,解決方法,結(jié)合實例形式總結(jié)分析了常見的問題原因與對應(yīng)的解決方案,需要的朋友可以參考下2023-05-05基于nodejs res.end和res.send的區(qū)別
今天小編就為大家分享一篇基于nodejs res.end和res.send的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05配置node服務(wù)器并且鏈接微信公眾號接口配置步驟詳解
這篇文章主要介紹了配置node服務(wù)器并且鏈接微信公眾號接口配置步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下2019-06-06