简体   繁体   English

阿贾克斯正在躲避我。 如何执行一个简单的ajax请求?

[英]Ajax is eluding me. How to execute a simple ajax request?

Okay background i am a js and ajax noob. 好的背景,我是js和ajax noob。 I work mainly in php and mysql html 5, i decided to get with the times and stop having to reload the page for simple things. 我主要在php和mysql html 5中工作,我决定与时俱进,不再为简单的事情而重新加载页面。

I am trying to make a simple ajax request to a php file and print the result. 我正在尝试向php文件提出一个简单的ajax请求并打印结果。

So on my page i have a text bax with the id password(leftover from another experiment) a jquery ui tabs setup with a tab with the id galleries and a div with the id my-galleries. 因此,在我的页面上,我有一个带有id密码的文本bax(另一个实验的剩余部分),一个jQuery ui选项卡设置,一个带有id画廊的选项卡和一个带有id my-galleries的div。 The idea is that upon click on the galleries tab the value of the password field is pulled and sent to the php file my-galleries.php and the result is printed to the div my-galleries and displayed. 想法是,单击“画廊”选项卡后,密码字段的值将被提取并发送到php文件my-galleries.php,结果将打印到div my-galleries并显示。

The code: 编码:

<input type="password" id="password"/>    
<div id="tabs">
    <ul>
        <li id="welcome"><a href="content/welcome.html">Welcome</a></li>
        <li><a href="content/about.html">About</a></li>
        <li><a href="#my-galleries" id="galleries">Galleries</a></li>
        <li><a href="content/contact.html">Contact</a></li>
    </ul>
    <div id="my-galleries"></div>
</div>

Then the js 然后js

$("#galleries").click(function(){
    $.ajax({
        type : 'POST',
        url : 'my-galleries.php',
        dataType : 'json',
        data: {
            password : $('#password').val()
        },
        success: function(msg){
            $("#my-galleries").html(msg)
        }           
    })
});

Then the php 然后PHP

$password= $_REQUEST['password'];
$salt= uniqid();
$str= $salt.$password;
$hash= hash("sha512", $str);
echo $hash;

According to the tuts i have been reading it should work but it doesn't. 根据我读过的评论,应该可以,但是不能。 I cant figure it out. 我弄清楚了。

Your problem is that you're requesting JSON; 您的问题是您正在请求JSON; your data is definitely not JSON. 您的数据绝对不是JSON。 It's plain text, so use the 'text' dataType : 它是纯文本,因此请使用'text' dataType

$("#galleries").click(function(){
    $.ajax({
        type: 'POST',
        url: 'my-galleries.php',
        dataType: 'jsontext',
        data: {
            password : $('#password').val()
        },
        success: function(msg) {
            $("#my-galleries").html(msg)
        }
    });
});

Use debugging tools from your browser, eg. 使用浏览器中的调试工具,例如。 Firebug in Firefox or Developer Tools in Google Chrome / Chromium. Firefox中的Firebug或Google Chrome / Chromium中的开发人员工具。

This will show you possible problems. 这将向您显示可能的问题。 They may be the following: 它们可能是:

  1. There is an error in your JavaScript. 您的JavaScript错误。
  2. You may be asking wrong destination. 您可能会问错目的地。
  3. There may be a redirection after request. 请求后可能会有重定向。
  4. There may be an error on the server (resulting in success() callback not being executed). 服务器上可能存在错误(导致未执行success()回调)。 Check the response using debugging tools. 使用调试工具检查响应。
  5. The data type may not be what you have specified. 数据类型可能不是您指定的。 Check if it is JSON. 检查是否为JSON。
  6. $ may not be defined. $可能未定义。 Check if it is. 检查是否。
  7. Your JS may be executed before the DOM objects (specifically the one with " galleries " ID) are available. 您的JS可能在DOM对象(特别是具有“ galleries ” ID的对象)可用之前执行。 Put your code eg. 把你的代码例如。 in $(function(){/* your code here */}); $(function(){/* your code here */}); .
  8. etc. 等等

You should change 你应该改变

echo $hash;

to

echo json_encode(array('response' => $hash));

then access it like so in your success block: 然后像这样在成功块中访问它:

$("#my-galleries").html(msg.response);

assuming you really want json for some reason. 假设您出于某种原因真的想要json。 Otherwise definitely use minitech's answer . 否则,一定要使用minitech的答案

...also, just my opinion, you should die($hash); ...此外,就我个人而言,你应该die($hash); and not echo $hash; 而不echo $hash; : kill the script on response. :在响应时终止脚本。

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

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