node.js - jsonp跨域为何get不到想要的内容

【字号: 日期:2022-09-12浏览:35作者:雯心

问题描述

我看过博客例子,尝试过跨域成功了。我用nodejs模拟了一下,改成了本地地址为何取不到zanNum中的内容?nodejs打开配置都没问题。结果:alert(‘fail’)

$(function(){ $.ajax({ type: 'get', async: false, url: 'http://192.168.191.1:3000/zanNum', dataType: 'jsonp', jsonp: 'callback', jsonpCallback:'response', success: function(response){ alert(response); }, error: function(){ alert(’fail’); } }); });

问题解答

回答1:

jsonp使用get和post的方式均以给出,可参考

<script>$(document).ready(function(){ $('#search').click(function(){ $.ajax({ type: 'GET', url: 'http://127.0.0.1:8000/ajaxdemo/serverjsonp.php?number=' + $('#keyword').val(), dataType: 'jsonp', jsonp: 'callback', success: function(data) {if (data.success) { $('#searchResult').html(data.msg);} else { $('#searchResult').html('出现错误:' + data.msg);} }, error: function(jqXHR){ alert('发生错误:' + jqXHR.status); }, }); });$('#save').click(function(){ $.ajax({ type: 'POST', url: 'http://127.0.0.1:8000/ajaxdemo/serverjsonp.php', data: {name: $('#staffName').val(), number: $('#staffNumber').val(), sex: $('#staffSex').val(), job: $('#staffJob').val() }, dataType: 'json', success: function(data){if (data.success) { $('#createResult').html(data.msg);} else { $('#createResult').html('出现错误:' + data.msg);} }, error: function(jqXHR){ alert('发生错误:' + jqXHR.status); }, }); });});</script>

相关文章: