jquery - jsonp覆盖回调获取参数
我正在编写一个动态 js,它应该向操作发送请求,该操作在链接的 href 属性中定义。
该链接看起来像这样:
<a href="module/action?callback=MyCallback">show something</a>
正如您所看到的,该链接有一个名为回调的获取参数。这是定义的,因为请求方法应该尽可能通用,并且元素本身应该知道它与响应有什么关系。
现在,我使用 jquery ajax 函数将请求发送到操作,如下所示:
jQuery('#mylink').live("click", function() { jQuery.ajax({ type: 'GET', dataType:"jsonp", error: ErrorHandler.catchXhrError, url: jQuery('#mylink').attr('href'); }); });
原则上,效果非常好。但我的问题是,jquery 定义了自己的回调 get 参数,我不知道如何告诉该方法,链接 href 中的回调参数是正确的,并且 request-uri 看起来像这样
http:/example.com/module/action?callback=MyCallback&callback=jsonp1279196981233
:我看到的唯一出路是定义我自己的名为 mycallback 的回调参数,并在操作中解释 mycallback 而不是回调:
<a href="module/action?callback=MyCallback">show something</a>
但我不喜欢这个解决方案,因为我的请求 uri 有一个不会使用的参数:
http:/example.com/module/action?mycallback=MyCallback&callback=jsonp1279196981233
请参加,我无法在 ajax 方法中定义回调方法的名称,
jQuery('#mylink').live("click", function() { jQuery.ajax({ type: 'GET', dataType:"jsonp", jsonpCallback: 'MyCallback', error: ErrorHandler.catchXhrError, url: jQuery('#mylink').attr('href'); }); });
因为 send 方法应该是通用的,并且将在我们的整个方面使用,并且响应必须由不同的 js 方法以多种不同的方式处理。
希望我能充分描述我的问题,并会很高兴获得任何帮助。
I'm writing a dynamic js that should send a request to an action, that is defined in the href attribute in a link.
The link look like that:
<a href="module/action?callback=MyCallback">show something</a>
As you can see the link has a get param named callback. That is defined, because the request method should be as generic as possible and the element should know by itself, what it has to do with the response.
Now i send the request with jquery ajax function to the action, looking like that:
jQuery('#mylink').live("click", function() { jQuery.ajax({ type: 'GET', dataType:"jsonp", error: ErrorHandler.catchXhrError, url: jQuery('#mylink').attr('href'); }); });
That works very well, in principle. But my problem is, that jquery defines its own callback get param and i don't know, how i can tell the method, that the callback param in my links href is the right one and the request-uri is looking like that:
http:/example.com/module/action?callback=MyCallback&callback=jsonp1279196981233
The only way out i see, is to define my own callback-param named mycallback and interpret mycallback in the action instead of callback:
<a href="module/action?callback=MyCallback">show something</a>
But i don't like this solution, because my request uri has a param that will not be used:
http:/example.com/module/action?mycallback=MyCallback&callback=jsonp1279196981233
Please attend, that i could not define the name of the callback method inside the ajax method
jQuery('#mylink').live("click", function() { jQuery.ajax({ type: 'GET', dataType:"jsonp", jsonpCallback: 'MyCallback', error: ErrorHandler.catchXhrError, url: jQuery('#mylink').attr('href'); }); });
because the send Method should be generic and will be used across our whole side and the response has to be processed in many different ways by different js methods.
Hope i could describe my problem sufficiently and would be happy about any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
jQuery.ajax
版本,但不要对回调名称进行硬编码。相反,解析 href 属性并提取要使用的回调的名称..这是解析..
You should use the
jQuery.ajax
version but not hardcode the callback name. Instead parse the href attribute and extract the name of the callback to use..and here is the parsing..
您是否查看过 http://code.google.com/p/jquery-jsonp/< /a>?
Have you looked into http://code.google.com/p/jquery-jsonp/ ?