简体   繁体   English

我要去哪里错了-Ajax

[英]Where am i going wrong - ajax

I have the following link- 我有以下链接-

<a href="ex.com" onClick="return popitup2('<?php echo $var3; ?>');">Grab Coupon</a>

Where i have initialized $var3 like this 我在哪里像这样初始化$ var3

$var3 = "brand1,camp2";

The code for the function popitup2() is - 函数popitup2()的代码是-

<script language="javascript" type="text/javascript">
function popitup2(id) {
$.ajax({
   url: "http://jainkunal.com/wordpress/wp-content/trackclicks/clickcounter.php"+"?"+id,
   success: function(){
     alert( "Data Saved: " );
   }
 });
    newwindow2=window.open('','name','height=225,width=350');   
var tmp = newwindow2.document;

....some more code... ...at end... ....更多代码... ...最后...

    return true;
}
</script>

Now when i click the link ex.com opens up without any alert ie without running the php script through ajax and the javascript after that. 现在,当我单击链接ex.com时,它不会发出任何警报,即不会在之后通过ajax和javascript运行php脚本。 If i remove the ajax call from the function popitup2() then the remaining javascript gets executed correctly. 如果我从函数popitup2()中删除了ajax调用,则其余的javascript将正确执行。

Agree with previous answer that you are executing asynchronous Ajax request. 同意您正在执行异步Ajax请求的先前答案。 From documentation Async parameter may not work in 2 cases: Cross-domain requests or if dataType: "jsonp". 从文档中,异步参数可能在两种情况下不起作用:跨域请求或dataType:“ jsonp”。 If you are doing crossdomain request, I can suggest only: 如果您要进行跨域请求,则只能提出以下建议:

<a href="ex.com" onClick="return popitup2('<?php echo $var3; ?>', this);">Grab Coupon</a>
<script type="text/javascript">
function popitup2(id, link) {
  $.ajax({
    url: "http://jainkunal.com/wordpress/wp-content/trackclicks/clickcounter.php"+"?"+id,
    context: link,
    success: function(){
      alert( "Data Saved: " );
      window.location = $(this).attr("href");
   }
  ....
  return false;
});

With such approach we track clicking for sure. 通过这种方法,我们可以确定跟踪点击。 There is another problem with such approaches, that tracking server should be fast otherwise, user will wait long time till navigate to resource. 这种方法还有一个问题,那就是跟踪服务器应该很快,否则,用户将等待很长时间才能导航到资源。

What's happening here is that you're performing an asynchronous AJAX request, meaning that when you perform the request, the rest of your function continues to run. 这里发生的是您正在执行异步AJAX请求,这意味着当您执行请求时,其余功能将继续运行。 When the AJAX result comes back, it then fires the alert in your success function, but since you've clicked a link, you've navigated away from that page already. 当AJAX结果返回时,它会在您的success功能中触发alert ,但是由于您单击了链接,因此您已经离开了该页面。

Try adding an async: false to the ajax function's parameters to wait for the result to come back before continuing, like so: 尝试在ajax函数的参数中添加async: false ,以等待结果返回后再继续,例如:

$.ajax({
    url: "http://jainkunal.com/wordpress/wp-content/trackclicks/clickcounter.php"+"?"+id,
    async: false,
    success: function() {
       alert( "Data Saved: ");
    }
 });

You are passing two arguments to JS function. 您正在将两个参数传递给JS函数。 But function prototype (first line) accept only one. 但是函数原型(第一行)仅接受一个。 This lead into JS error. 这导致JS错误。

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

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