UPDATE THREE
由于我从自己的api中获得了正确的响应,我可以修改我得到的响应吗。在控制台中,我看到:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort: ƒ (a)
always: ƒ ()
complete: ƒ ()
done: ƒ ()
error: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ (a)
overrideMimeType: ƒ (a)
pipe: ƒ ()
progress: ƒ ()
promise: ƒ (a)
readyState: 4
responseText: "{"success":true}
↵<!DOCTYPE html>
↵<html lang="en-"
setRequestHeader: ƒ (a,b)
state: ƒ ()
status: 404
statusCode: ƒ (a)
arguments: null
caller: null
length: 1
name: "statusCode"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery.js?ver=1.12.4-wp:4
[[Scopes]]: Scopes[3]
statusText: "Not Found"
success: ƒ ()
then: ƒ ()
__proto__: Object
 我试过使用
status_header(); 在我的php中,但这不起作用。
UPDATE TWO
问题是否与此相关:
https://stackoverflow.com/questions/3445270/jquery-ajax-returning-404-error-but-correct-response如果是这样,我不知道该怎么办。。。
UPDATE
如果我使用
test_login_file.php  作为AJAX url,我可以再次包含以下行:
defined( \'ABSPATH\' ) or die( \'No access this way\' );
add_shortcode(\'myLogin\', \'myLogin\');
 删除荒谬的:
aCompletelyUnlikelyfunction();
 在这种情况下,cookie已设置,页面不会刷新(到目前为止很好…),但控制台中的url出现404 not found错误。此响应包括预期的
{"success": true} JSON,但404页面也有大量HTML,因此AJAX响应处理不起作用-我显然需要做其他事情。
很明显,这必须是“ABSPATH”,否则它会像以前一样抛出错误。
到底怎么回事?这是一个WordPress的问题,还是我根本不理解什么?
感谢您的关注!
我试图使用谷歌云平台上通过Bitnami安装的WordPress来构建网站,但使用另一个服务器(服务器B)上的系统来管理用户和内容。我希望能够将用户登录到我的B服务器,获取JSON web令牌,并使用该令牌对站点其他部分的用户进行身份验证。
下面的代码可以工作,并使用我的令牌值设置一个安全的httpOnly cookie。
但是,为了做到这一点,我必须使用错误的行来停止ajax:
aCompletelyUnlikelyfunction();
 否则它不会设置cookie(可能是因为AJAX导致了一些输出?)。这也会导致页面刷新。
如果我删除这一行,AJAX将正常工作,根据我提交的内容提供适当的弹出窗口。但正如上面所指出的,它并没有设定cookie。
为了让AJAX正常工作,我不得不对这些行进行注释:
//defined( \'ABSPATH\' ) or die( \'No access this way\' );
//add_shortcode(\'myLogin\', \'myLogin\');
 表格:
html
      <form id="loginform" name="loginform" method="post">
         <div>
            Email:
            <input type="text" name="email" id="email" />
            Password:
            <input type="password" name="password" id="password" />
            <input type="hidden" name="call" id="call" value="1"/>
            <input type="submit" name="loginBtn" id="loginBtn" value="Login" />
         </div>
      </form>
 jQuery:
<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery(\'#loginform\').submit(function(e) {
        e.preventDefault();
        jQuery.ajax({
            type: "POST",
            url: \'/wp-content/plugins/test_login_file.php\',
            data: jQuery(this).serialize(),
            aCompletelyUnlikelyfunction(); //I know this is very wrong!
            success: function(response){
                console.log(response);
                var jsonData = JSON.parse(response);
 
                if (jsonData.success === true)
                {
                    alert(\'Logged In\');
                }
                else
                {
                    alert(\'Invalid Credentials!\');
                }
           },
           error: function(error){
                console.log("Error");
                console.log(error);
           }
           
       });
     });
});
     </script>
 和php:
<?php
/**
 * Plugin Name: Test Login
 * Plugin URI: 
 * Description: Test Login
 * Version: 1.0
 * Author: PMP
 * Author URI:
 **/
//defined( \'ABSPATH\' ) or die( \'No access this way\' );
//add_shortcode(\'myLogin\', \'myLogin\');
function get_token()
{  
    ob_start();
    $method  = \'POST\';
    $url     = \'https:my_B_server/session\';
    $headers = array(
        \'Accept: application/json\',
        \'Content-Type: application/json\'
    );
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($_REQUEST));
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_SSLVERSION, 4);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
        
    if ($code == 200) {
        
        /*if (isset($_COOKIE["my_token"]))
        {
        echo json_encode(array(
        "success" => \'Logged In\'
        ));
        }
        else*/
        //{
        $response = json_decode($response, true);
        $token    = $response["session_token"];
        $expiry   = strtotime(\'+1 month\');
        setcookie("my_token", $token, $expiry, COOKIEPATH, COOKIE_DOMAIN, true, true);
        $thing = json_encode(array(
            "success" => true
        ));
        //}
    } else {
        
        $response = json_decode($response, true);
        $context  = $response["error"]["message"];
        $token    = null;
        setcookie("my_token", "error", time() - 3600);
        
        $thing = json_encode(array(
            "error" => $context
        ));
    }
    echo $thing;
    ob_end_flush();
}
if (isset($_POST[\'call\'])) {
    $pmp = $_POST[\'call\']; {
        if ($pmp == 1) {
            get_token();
        }
    }
}