javascript 将 $.ajax 分配给变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18060564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:37:03  来源:igfitidea点击:

Assign $.ajax to variable

javascriptjqueryajax

提问by jake

I'm not getting any output the id_function.php works perfectly fine can anyone please help me?

我没有得到任何输出 id_function.php 工作得很好 任何人都可以帮助我吗?

JavaScript

JavaScript

 function get_details()
    {
        var oriz = document.getElementById("from").value;
        var dizz = document.getElementById("to").value;

        var id_orig_diz = $.ajax({
            type: "POST", 
            url: 'id_function.php?orig='+oriz+'&des_id='+dizz,
            dataType: "text", 
            async: false
                        }).responseText;
        alert(id_orig_diz); 

    }

This is my PHP but I'm not getting any id

这是我的 PHP 但我没有得到任何 ID

            <?php
            $name_1 = $_GET['orig'];
            $name_2 = $_GET['des_id'];
            try {
                $dbuser = "kim";
                $dbpass = "kim";
                $conn = new PDO('mysql:host=localhost;dbname=destination', $dbuser, $dbpass);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
                $stmt = $conn->prepare("SELECT pl_id FROM view_places WHERE name = :name LIMIT 1");

                $stmt->bindParam(':name',$name_1); 
                $stmt->execute();
                $result_1 = $stmt -> fetch();
                $res1 = $result_1["pl_id"];  

                $stmt->bindParam(':name', $name_2); 
                $stmt->execute(); 
                $result_2 = $stmt -> fetch(); 
                    $res2 = $result_2["pl_id"];  
                    echo   'origin_number:'.$res1. ', '.'destination_id:'.$res2;
                }   catch(PDOException $e) {
                        echo 'ERROR: ' . $e->getMessage();
                }

            ?>

回答by Alessandro Minoccheri

try instead this because when you make the alert the ajax call can't be finish yet. With the suucess function you are secure that the ajax call is finished, you have to echo something into your php file if you want a response:

试试这个,因为当你发出警报时,ajax 调用还不能完成。使用 suucess 函数,您可以确保 ajax 调用已完成,如果您想要响应,您必须将某些内容回显到您的 php 文件中:

$.ajax({
   type: "POST", 
   url: 'id_function.php',
   dataType: "text", 
   data: { 'orig': oriz, 'des_id' : dizz },
   async: false,
   success: function(data){
      alert(data);
   }
})

Into your php file you can take values in this mode:

在你的 php 文件中,你可以在这种模式下取值:

<?php
$orig = $_POST['orig'];
$des_id = $_POST['des_id'];
?>

回答by Justin Ethier

The recommended method is to use a successfunction to receive the data and assign your ID. From the documentation:

推荐的方法是使用一个success函数来接收数据并分配您的 ID。从文档

success

Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )

成功

类型:函数(PlainObject 数据,字符串 textStatus,jqXHR jqXHR)

You should use the dataparameter to get your ID.

您应该使用该data参数来获取您的 ID。



The code is the same as in Alessandro's answer:

代码与亚历山德罗的回答相同:

success: function(data){
    alert(data);
}

回答by Mike Thomsen

This will not work because AJAX calls are asynchronous which means that they don't immediately return a value after you execute them. It has to go out to the server, let the serverside code run and then provide a value back. Think of it as similar in many ways to kicking off a thread in another language; you have to wait for the thread to yield control back to your application.

这是行不通的,因为 AJAX 调用是异步的,这意味着它们在执行后不会立即返回值。它必须到达服务器,让服务器端代码运行,然后返回一个值。可以将其视为在许多方面与使用另一种语言启动线程相似;您必须等待线程将控制权交还给您的应用程序。

var id_orig_diz = $.ajax({
    type: "POST", 
    url: 'id_function.php?orig='+oriz+'&des_id='+dizz,
    dataType: "text", 
    async: false
}).responseText;

What you need to do is set a result handler like this:

您需要做的是设置一个这样的结果处理程序:

var id_orig_diz;
$.ajax({
    type: "POST", 
    url: 'id_function.php?orig='+oriz+'&des_id='+dizz,
    dataType: "text", 
    async: false,
    success: function(data){
        id_orig_diz = data; //or something similar
    }
});