jquery - jsonp覆盖回调获取参数

发布于 2024-09-09 06:11:33 字数 1380 浏览 4 评论 0原文

我正在编写一个动态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热情消退 2024-09-16 06:11:33

您应该使用 jQuery.ajax 版本,但不要对回调名称进行硬编码。相反,解析 href 属性并提取要使用的回调的名称..

这是解析..

jQuery('#mylink').live("click", function() {
    var href = jQuery('#mylink').attr('href');

    jQuery.ajax({
      type: 'GET',
      dataType:"jsonp",
      jsonpCallback: href.match(/([?&]callback[a-zA-Z0-9=]+)/)[0].split('=')[1],
      error: ErrorHandler.catchXhrError,
      url: 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..

jQuery('#mylink').live("click", function() {
    var href = jQuery('#mylink').attr('href');

    jQuery.ajax({
      type: 'GET',
      dataType:"jsonp",
      jsonpCallback: href.match(/([?&]callback[a-zA-Z0-9=]+)/)[0].split('=')[1],
      error: ErrorHandler.catchXhrError,
      url: href;
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文