简体   繁体   English

为什么我的浏览器在post ajax调用中发送两个请求?

[英]Why does my browser send two requests in a post ajax call?

I have put a link in my web page with the following a tag: 我在网页中放置了带有以下标签的链接:

<a href="" onclick="FiltrarxUnidadPolitica(1)">
   <label class="filter-item-title">
      Distrito Capital
   </label>
   <label class="filter-item-quantity">
      (3)
   </label>
</a>

And the js function that is called when the link is clicked is as follow: 单击链接时调用的js函数如下:

function FiltrarxUnidadPolitica(iIdUnidadPolitica, bLimpiarPropiedad) {
   $.ajax({
      data: { iIdUnidadPolitica, bLimpiarPropiedad },
      url: "/Home/ListadoInmuebles",
      type: 'POST',
   });
}

The issue that I have is when the link is clicked and the js function is executed the browser makes two requests to the server as you can see in the picture inserted (pending calls), the first one the POST call included in the js function and the second one that is a GET call that I don't know how is initiated. 我遇到的问题是,单击链接并执行js函数时,浏览器向服务器发出两个请求,如您在插入的图片中看到的(待处理的调用),第一个请求是js函数中包含的POST调用,第二个是我不知道如何启动的GET调用。

浏览器网络调用

Does anyone know where is comming from the GET call? 有人知道GET呼叫的来向吗?

Could anyone give me a clue about where I have to look for? 谁能给我一个关于我要去哪里找的线索?

Your link is being followed by the browser, because you haven't told it not to follow it. 浏览器正在跟踪您的链接,因为您没有告诉它不要跟踪它。 ( href="" is the same as "the current page.") href=""与“当前页面”相同。)

Several ways you can prevent that: 有几种方法可以防止这种情况:

  • Don't use a link for something that isn't a link, use an input type="button" or button and style it to look how you want it to look (this will also help with assistive software). 不要将链接用于不是链接的东西,请使用input type="button"button并对button进行样式设置以使其看起来像您的外观(这也将对辅助软件有所帮助)。
    (If you use button , make sure it's either not in a form or it has type="button" ; the default type of buttons is, bizarrely, "submit" .) (如果使用button ,请确保它不是form或者具有type="button"type="button"的默认类型奇怪的是"submit" 。)

  • Hook up your function with jQuery instead of an onclick attribute and call preventDefault on the event object. 使用jQuery而不是onclick属性连接函数,并在事件对象上调用preventDefault

  • Set href to javascript:; href设置为javascript:;

     <a href="javascript:;" onclick="FiltrarxUnidadPolitica(1);"> 

    (Or to href="javascript:void(0)" .) (或转到href="javascript:void(0)" 。)

  • Set href to #anchor-that-does-not-exist href设置为#anchor-that-does-not-exist

    (Don't use href="#" unless you want the page to scroll to the top when the link is followed.) (除非使用该链接,否则不要使用href="#"除非您希望页面滚动到顶部。)

  • Add a return false to the onclick : onclick添加return false

     <a href="" onclick="FiltrarxUnidadPolitica(1); return false;"> 

    Returning false from a DOM0 attribute-style handler prevents the default action; 从DOM0属性样式处理程序返回false阻止默认操作; more on my blog: The true story on return false . 有关我的博客的更多信息: return false的真实故事

Probably others as well... 可能还有其他人...

Preflighted requests 事前要求

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. 与简单请求(如上所述)不同,“预检”请求首先将HTTP OPTIONS请求标头发送到另一个域上的资源,以确定实际请求是否可以安全发送。 Cross-site requests are preflighted like this since they may have implications to user data. 跨站点请求这样被预检,因为它们可能会影响用户数据。 In particular, a request is preflighted if: 特别是在以下情况下,请求将被预检:

It uses methods other than GET or POST. 它使用GET或POST以外的方法。 Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, eg if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted. 同样,如果POST用于发送请求类型为application / x-www-form-urlencoded,multipart / form-data或text / plain之外的Content-Type的请求数据,例如POST请求将XML有效负载发送给服务器使用application / xml或text / xml,则对请求进行预检。 It sets custom headers in the request (eg the request uses a header such as X-PINGOTHER) 它在请求中设置自定义标头(例如,请求使用标头,例如X-PINGOTHER)

Source: MDN 资料来源:MDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM