在 jQuery 中,使用 ajaxSend 预览 $.post 调用构建的 url

Posted

技术标签:

【中文标题】在 jQuery 中,使用 ajaxSend 预览 $.post 调用构建的 url【英文标题】:In jQuery, using ajaxSend to preview the url built by $.post call 【发布时间】:2010-09-09 14:07:01 【问题描述】:

如何构造我的 ajaxSend 调用,这似乎是放置它的地方,以预览传递回代理的内容?另外,我可以在 ajaxSend 中停止 ajax 调用吗?..所以我可以在处理来自代理的错误之前完善我的 url 字符串?

这是完整的 URL,当它传递给代理时,它将返回我需要的 JSON 代码:

http://myServer/cgi-bin/broker?service=myService&program=myProgram&section=mySection&start=09/08/08&end=09/26/08

这是我的 $.post 调用(不确定它是否创建了上面的 url 字符串)

$(function() 
 $("#submit").bind("click",
  function() 
   $.post(
     url: "http://csewebprod/cgi-bin/broker" ,
     datatype: "json",
     data: 
      'service' : myService,
      'program' : myProgram,
      'section' : mySection,
      'start' : '09/08/08',
      'end' : '09/26/08'
      ,
     error: function(request)
      $("#updatehtml").removeClass("hide") ;
      $("#updateHTML").html(request.statusText);
      ,
     success: function(request) 
      $("#updateHTML").removeClass("hide") ;
      $("#updateHTML").html(request) ;
      
    ); // End post method
   ); // End bind method
  ); // End eventlistener

谢谢

【问题讨论】:

【参考方案1】:

预览正在发送的 HTTP 请求的一种简单方法是在 Firefox 中使用 Firebug。下载并启用插件,当请求发出时,它会显示在 firebug 控制台中。

【讨论】:

谢谢,萤火虫帮助我确保我向服务器发送了正确的 url【参考方案2】:

API/1.2/Ajax 提供有关如何绑定到 AJAX 事件的信息。

// Hook into jQuery's ajaxSend event, which is triggered before every ajax
// request.
$(document).ajaxSend(function(event, request, settings) 
  // settings.data is either a query string like "id=1&x=foo" or null

  // It can be modified like this (assuming AUTH_TOKEN is defined eslewhere)
  settings.data =
    ((settings.data) ? settings.data + "&" : "") +
    "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
);

【讨论】:

以上是关于在 jQuery 中,使用 ajaxSend 预览 $.post 调用构建的 url的主要内容,如果未能解决你的问题,请参考以下文章

使用 ajaxSend 在 iframe 中捕获来自父页面的请求

jquery中各个事件执行顺序如下:

在 django 中使用 jQuery 预览 Ajax 头像

jquery的AJAX中各个事件执行顺序

用于文档预览的 Jquery 脚本?

Jquery监听AJAX请求