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
如果是這樣,我不確定我该怎麼做...
UPDATE
如果我只用
test_login_file.php
作為AJAX網址,我可以再次添加以下行:
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安裝在Google Cloud Platform上的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();
}
}
}
- 5月前1 #
相似問題
- wordpress:AJAX表單發佈返迴0wordpressplugindevelopmentwordpressajax2021-01-02 07:55
- wordpress:$ wpdb使用ajax但顯示ajax錯誤而不是成功wordpresspluginswordpressplugindevelopmentwordpressajax2020-11-17 00:53
- wordpress:確保功能已完成,然後再允许再次呼叫Ajaxwordpressplugindevelopmentwordpressajaxwordpressusermeta2020-08-26 00:54
- wordpress:如何获得我的wordpress插件以接收資料並將其通過ajax / php請求中繼到需要身份驗證的遠端服務器?wordpressphpwordpressplugindevelopmentwordpressajax2020-06-26 04:23
- wordpress:Ajax响應未收到wordpresscustomposttypeswordpressplugindevelopmentwordpressajax2020-05-27 19:26
迴答我自己的問题似乎很奇怪,但是如果有人有用,這就是答案。
要對插件进行Ajax呼叫並正確接收响應,必须在插件中註册该端點.這使您可以避開wordpress所提供的保護.我不確定這裏是否允许/接受到youTube的鏈接,但是這裏有一个很棒的视频.如果這被视為不良做法,我將删除。
php for plugin