简体   繁体   English

我的AJAX代码有什么问题

[英]What is wrong with my AJAX code

I am brand new to the world of AJAX. 我是AJAX领域的新手。 I have a Mysql database that contains a column filled with URLs. 我有一个Mysql数据库,其中包含一个用URL填充的列。 My end goal is to have an on click event load an iframe with a unique URL from the database. 我的最终目标是使点击事件从数据库中加载具有唯一URL的iframe。 If anyone has a better methodology please let me know. 如果有人有更好的方法,请告诉我。

Right now however I'm just trying to figure out how AJAX works by trying to retrieve the URLs. 但是现在,我只是通过尝试检索URL来弄清楚AJAX的工作方式。 I've attached my Javascript code and my PHP code. 我已经附加了我的Javascript代码和我的PHP代码。

The PHP code does output a json encoded copy of the data. PHP代码确实输出数据的json编码副本。 The javascript however reports the variable result as undefined. 但是,javascript将变量结果报告为未定义。 I think this may have to do with the "asynchronous" side of AJAX but I followed this tutorial to try to make code work correctly. 我认为这可能与AJAX的“异步”方面有关,但是我按照本教程尝试使代码正确工作。 I appreciate any help that can be provided. 感谢提供的任何帮助。

How do I return the response from an asynchronous call? 如何从异步调用返回响应?

Here is my code Javascript code it logs results is undefined and "I made it" to the console 这是我的代码Javascript代码,它记录结果是不确定的,并且“我成功”到控制台

function retrieve_callback(result) { 
    console.log (result);
    console.log("I made it!");
    };

function retrieveURL (retrieve_callback) {  
    $.getJSON({
        url: './fetch.php',
        dataType: 'json',
        success: retrieve_callback
    });
}

//Runs when our document initializes
$( document ).ready(function() {
    retrieveURL(retrieve_callback());
});

Here is my PHP code it outputs the JSON array of the URLs 这是我的PHP代码,它输出URL的JSON数组

<?php
//--------------------------------------------------------------------------
// Connect to mysql database (I've removed the info from this example but it works)
//--------------------------------------------------------------------------

$db = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

//--------------------------------------------------------------------------
// Query mysql database
//--------------------------------------------------------------------------

$links = array();

//SQl query
$sql = <<<SQL
    SELECT `Gnews_url`
    FROM `Gnews_RSS`
SQL;

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){
    array_push($links, $row);
}

echo json_encode($links);

retrieveURL(retrieve_callback()); is incorrect. 是不正确的。

retrieveURL is expecting you to pass a function reference so it can execute it once the request completes. retrieveURL希望您传递一个函数引用,以便请求完成后可以执行它。 Instead, you're executing retrieve_callback and passing the result of that ( undefined ) to retrieveURL , so it never actually does anything when the request completes. 相反,您正在执行retrieve_callback ,并将该结果( undefined )传递给retrieveURL ,因此在请求完成时,它实际上从不执行任何操作。 Not only that, but you're executing the callback before you make the request. 不仅如此,您还可以在发出请求之前执行回调。

You should pass it like: 您应该像这样通过它:

retrieveURL(retrieve_callback);

This: 这个:

$( document ).ready(function() {
    retrieveURL(retrieve_callback());
});

You are calling retrieveURL with the RESULTS of calling retrieve_callback(). 您正在使用带有retrieve_callback()的结果来调用retrieveURL。 Do this instead: 改为这样做:

$( document ).ready(function() {
    retrieveURL(retrieve_callback);
});
retrieveURL(retrieve_callback); // no brackets for retrieve_callback

In Javascript, methods are objects too. 在Javascript中,方法也是对象。 In this case you are passing the method retrieve_callback as object into retrieveURL. 在这种情况下,您要将方法retrieve_callback作为对象传递给retrieveURL。 retrieveURL on the other hand does assign the method retrieve_callback as the member "success" to a anonym object which is passed into $.getJSON(). 另一方面,retrialURL确实将方法“ retrieve_callback”作为成员“成功”分配给传递给$ .getJSON()的匿名对象。 This means that the anonym object does now have a method called "success" which works like retrieve_callback. 这意味着匿名对象现在确实具有一种称为“成功”的方法,其工作方式类似于retrieve_callback。

After the successful AJAX request, jquery uses the anonym object and calls its success-method like this: 在成功的AJAX请求之后,jquery使用匿名对象并调用其成功方法,如下所示:

blaObject.success(ajaxResult);

We know that "success" is a copy of "retrieve_callback", so the follwing will happen: 我们知道“成功”是“ retrieve_callback”的副本,因此将发生以下情况:

// instead of blaObject.success(ajaxResult);
retrieve_callback(ajaxResult);

Let's explain it in a short way (see se usage of the brackets): 让我们简短地解释一下(请参阅方括号的用法):

// Get the result of a call of retrieve_callback() and pass it to retrieveURL.
retrieveURL(retrieve_callback());

// Pass the method retrieve_callback itself  retrieveURL.
retrieveURL(retrieve_callback);

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

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