express ajax jsonp,javascript - Node.js(express) .end() with JSONP - Stack Overflow

博客介绍了JSONP的工作原理,特别是在客户端如何通过指定回调函数实现跨域数据请求。内容详细解释了如何在URL中添加回调参数,并在服务器端如何包装JSON响应以匹配这个回调函数,从而解决跨域问题。特别提到了jQuery在处理此类请求时的角色,以及如何获取并使用回调函数名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As user2840784 points out, you need the callback for this to work. To elaborate on their answer, the client library will need to specify a 'client side callback' when making the request for example:

http://my-service.com/get-data.json?callback=callThisFunction

If you're using jQuery on the client side, jQuery will provide the callback name for you when you make the $.ajax request, so your request will look like:

http://my-service.com/get-data.json?callback=jQuery123456789

Behind the scenes, jQuery secretly creates a function with the name jQuery123456789 (or something) to handle your data when it's loadded.

What you have to do is make sure you wrap your JSON output with the callback function name, so if your response JSON would look like this:

{"a":1, "b":2}

... then you need to wrap it so it looks like this:

jQuery123456789('{"a":1, "b":2}')

Again, as user2840784 pointed out, you can get the name of the callback from req.query.jsonp.

Hth,

Aaron

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值