簡體   English   中英

如何通過js文件中的ajax獲取請求參數?

[英]How can i get request parameters through ajax in js file?

我已將 ajax 請求發送到 js 文件,如下所示

 function add(lt,ln)

{

    $.ajax({

          type: 'get',
            url: "js/abc.js",
            data: {action: "addme", lt: lt,ln:ln},
            async: false,
            success: function(data){

            }
            });



}

現在的問題是我需要 abc.js 文件中的 lt 和 ln 變量。 發送此請求時如何獲取這些變量我想要 n abc.js 文件

if(action==addme)
{
var lt=set value which is comng from ajax request.
}

您不能在 JS 文件中執行此操作

發送 Ajax GET 請求后,url 將變為./js/abc.js?action=action&lt=lt 因此,您正在向 JS 文件發送參數,並且 JS 文件不會自動執行或更改,因為.js擴展名。 除外:當您更改服務器配置時

另一種方法是:將文件擴展名更改為.php.html或其他(JavaScript output in Z23BEC3922204A75C 文件)

喜歡:

function add(lt,ln)
{
    $.ajax({
          type: 'get',
          url: "js/abc.php",
          data: {action: "addme", lt: lt,ln:ln},
          async: false,
          success: function(data){
           ///Do something with 'data'
           ///'data': var lt=lt; alert(lt);
            }
            });
}

js/abc.php

<?php
   $action = $_GET["action"];
   if($action=="addme"){ //check if action=addme
      echo "var lt=".$_GET["lt"].";"; //Adding variable coming from Ajax request
    }?>
 alert(lt);

嘗試理解代碼。 希望對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM