简体   繁体   English

为什么AJAX不起作用?

[英]Why isn't AJAX working?

I'm trying to perform a simple ajax call to a simple php script to test that ajax is working. 我试图对一个简单的php脚本执行一个简单的ajax调用,以测试ajax是否正常工作。

HTML: HTML:

<button id="hi">hi</button>

JS: JS:

<script type="text/javascript">
$( "#hi" ).click(function(){
   var dataString = "hi";
   $.ajax({
     url: "custom_scripts/hi.php",
     type: "POST",
     dataType:'json',
     data: dataString,
     cache: false,
     success: function(data){
       if(data.auth==true){
         alert("success: " + data.auth);
       }         
     }
   }) 

}); });

PHP: PHP:

<?php

    $hi=$_POST['hi'];

    $rVal=array("auth" => false);

    if(isset($hi)){
        $rVal['auth']=true;
    } else {
        $rVal['auth']=false;
    }

    echo json_encode($rVal);

?>

Are there any other scripts or libraries (from any language) that can interfere with ajax calls? 是否有其他脚本或库(来自任何语言)可能会干扰ajax调用? When I step through, it's reaching the call, it's not returning any data (and no error in console) ... 当我逐步执行时,它就到达了呼叫,它没有返回任何数据(并且控制台中没有错误)...

You just need to pass Ajax data as: 您只需要按以下方式传递Ajax数据:

 data: "hi="+dataString,

Complete Ajax Example: 完整的Ajax示例:

$( "#hi" ).click(function(){
   var dataString = "hi";
   $.ajax({
     url: "custom_scripts/hi.php",
     type: "POST",
     dataType:'json',
     data: "hi="+dataString,
     cache: false,
     success: function(data){
       if(data.auth==true){
         alert("success: " + data.auth);
       }         
     }
   }) 
}); 

In your example, if you check browser console, you are getting Undefined Index Notice . 在您的示例中,如果您检查浏览器控制台,则会收到Undefined Index Notice

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

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