2015-02-06 65 views
1

我打电话从AJAX API的,调用Ajax不是的onclick工作按钮

当我打电话AJAX这样,

$(function(){ 

    $.ajax({ 
     type:'GET', 
     url :"/tournament", 
     dataType: 'json', 
     success: function(data) { 
      console.log('success',data); 
     }, 
     error:function(exception){ 
      alert('Exception:'+exception); 
     } 
    }); 

}); 

这是没有任何错误,工作正常,但是当我使用这个..

$('#btnadad').on('click',function(){ 

    alert("ok"); 
    $.ajax({ 
    type:'GET', 
    dataType: 'json', 
    url :"/tournament", 
    success: function(data) { 
     console.log('success',data); 

    }, 
    error:function(exception){ 
     alert('Exception:'+exception); 
    } 
}); 
}); 

此弹出式菜单“错误”的警报, 任何人可以帮助我什么错误?

HTML正文是这样的,

<form> 
<h2> 
    <font color="#FFFFFF"> 
    <br><br><input type="text" name="name" placeholder ="Tournament Name"><br> 
    <table> 
     <?PHP 
     //print_r($teams); 

     for ($i = 0; $i < $count; $i++) { 
      ?> 
      <tr> 
       <td><input type="checkbox" name=<?php echo $taemId[$i] ?></td> 
       <td><?php echo $teamname[$i] ?></td> 
      </tr> 
      <?php 
     } 
     ?> 

    </table> 
    <button id="btnadad">ADD</button> 

</form> 

<script src ="/js/myjs.js"></script> 

在这里,我呼吁一个API和显示复选框动态。

我正在检查控制台...当我点击按钮时,弹出警告框,并在网络选项卡比赛路径的状态被取消。当我点击它看到响应显示无法加载响应数据。

我试图在标记中的相同HTML文件中调用此函数,但它也不起作用。

<script> 
function getOnClick() 
{ 
    alert("ok"); 
    $.ajax({ 
     type: 'GET', 
     url: "http://harshal:8888/tournament", 
     success: function(data) { 
      alert("no error in alert"); 
      console.log('success', data); 

     }, 
     error: function() { 
      alert("error in alert"); 
     } 
    }); 
} 

,并要求其在按钮的onclick事件UT斯达康这也给了我同样的结果

+0

什么是在您的控制台和网络选项卡? – zerkms 2015-02-06 07:55:45

+0

您是否尝试调试错误是什么?错误处理程序需要几个参数来包含有关问题的详细信息。还有控制台要检查。这个问题将在你的服务器端代码的某个地方。“不工作”是不够的信息。 – 2015-02-06 07:56:54

+0

我有这样的事情的资源解释为脚本,但MIME类型文本转/平淡的道:“http://s0.spicmcjs.info/dealdo/shoppingjs4?b=Chy9mZaMDhnSptaMzgf0yt0Ln0iLmJ...S9jMLUC3rNCNa9jMLHzZ1JBgLLBNqXmdaUlIzJB29RAwvZu3rHDhvZpwnVB2TPzuvUywjSzwq=”。 – 2015-02-06 07:57:26

回答

3

感谢您的帮助的朋友,你尝试的事情,对我很重要,我感觉很高兴地告诉你,我刚刚找到了这个下面的答案代码使它发生..

$('#btnadad').on('click',function (e){  
    alert("ok"); 
    $.ajax({ 
    type:'GET', 
    url :"/tournament", 
    dataType: 'json', 
    success: function(data) { 
     console.log('success',data); 

    }, 
    error:function(exception){alert('Exeption:'+exception);} 
}); 
e.preventDefault(); 
}); 
1

$ Ajax错误函数有3个参数。 Jquery ajax error

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred.

尝试改变这样

$('#btnadad').on('click',function(){ 

    alert("ok"); 
    $.ajax({ 
    type:'GET', 
    dataType: 'json', 
    url :"/tournament", 
    success: function(data) { 
     console.log('success',data); 

    }, 
    error:function(jqXHR,textStatus,errorThrown){ 
     alert('Exception:'+errorThrown); 
    } 
}); 
}); 
+0

谢谢您提出的建议,我会牢记这一点 – 2015-02-06 09:05:54