简体   繁体   English

在另一个ajax调用中循环的ajax调用中,从父URL(当前窗口URL)获取路径

[英]Get path from parent URL (current window URL) in ajax call looped inside another ajax call

I have a php page (client.php) contains the script below. 我有一个php页面(client.php)包含以下脚本。 The script performs an ajax call. 该脚本执行ajax调用。 Upon success, it executes another ajax call. 成功后,它将执行另一个ajax调用。 I'm having an issue with the second ajax call where it returns the request_uri as "sql/adminloademail.php" not the parent path which is "/client.php". 我在第二个ajax调用中遇到问题,在该调用中, request_uri返回为“ sql / adminloademail.php”,而不是“ /client.php”的父路径。 How do I tell php to echo the parent path "/client.php" which appears in the current window URL? 如何告诉php回显出现在当前窗口URL中的父路径“ /client.php”?

SCRIPT 脚本

$.ajax({
  method:'POST',
  url:'sql/adminaddclient.php',
  data:formData,
  contentType: false,
  processData: false,
  success: function(data){
    $.ajax({
      type:'POST',
      url:'sql/adminloademail.php',
      success: function(data){
        $('#account_list').html(data);
      }
    });
  }
});

PHP(adminloademail.php) PHP(adminloademail.php)

$page = parse_url(filter_input(INPUT_SERVER, 'REQUEST_URI' , FILTER_SANITIZE_STRING), PHP_URL_PATH);
echo $page;

You can set parent url to a variable and then send it with the data param. 您可以将父URL设置为变量,然后将其与数据参数一起发送。

var parentUrl = window.location.href;

$.ajax({
  type:'POST',
  url:'sql/adminaddclient.php',
  data:formData,
  contentType: false,
  processData: false,
  success: function(data){
    $.ajax({
      type:'POST',
      data:{"parentUrl":parentUrl},
      url:'sql/adminloademail.php',
      success: function(data){
        $('#account_list').html(data);
      }
    });
  }
});

Also you can try to get it from headers. 您也可以尝试从标题获取它。

$headers = apache_request_headers();
if (isset($headers['Referer'])) {
    echo "Referer: " . $headers['Referer'];
}

Since PHP 5.4.0 apache_request_headers also available under FastCGI. 从PHP 5.4.0开始, apache_request_headers也可以在FastCGI下使用。

Since PHP 5.5.7 apache_request_headers also available in the CLI server. 从PHP 5.5.7开始, apache_request_headers也可在CLI服务器中使用。

http://php.net/manual/en/function.apache-request-headers.php http://php.net/manual/zh/function.apache-request-headers.php

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

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