首页 > nodejs和ajax实现jsonp跨域问题

nodejs和ajax实现jsonp跨域问题

前端ajax请求代码:testJsonp.html

$(function(){
            $.ajax({
                dataType: 'jsonp',
                url: 'localhost:8088',
                type:'get',
                success: function(data){
                       $('#name').text(data.name);
                }
            });
        })

nodejs服务器代码:jsonp.js

var http = require('http');
var urlLib = require('url');
var data = {name:'luo',year:18};
http.createServer(function(req,res){
    var parms = urlLib.parse(req.url,true);
    var str = parms.query.callback + '(' + JSON.stringify(data) +')';
    res.end(str);
}).listen(8088);

然后我打开testJsonp.html 发现报错

请问我的代码有什么问题吗,谢谢了


urlhttp://localhost:8088


net::ERR_UNKNOWN_URL_SCHEME
http scheme带上

$.ajax({
    dataType: 'jsonp',
    url: 'http://localhost:8088',
    type:'get',
    success: function(data){
           $('#name').text(data.name);
    }
});

这个可以帮助你
用nodejs实现json和jsonp服务

【热门文章】
【热门文章】