【发布时间】:2017-06-30 00:15:40
【问题描述】:
我有一个应该是安全的应用程序,因此使用了 Nonce,但我无法让它们工作。 我已经尝试了几个选项,但由于验证不起作用,显然缺少一些东西。
我的js代码片段是:
function placeNew(next){
_nonce = $('input#_nonce').val();
request = $.ajax({
type: 'POST',
url: url_path + '/foo/v1/newbee',
data: {bid : next , _nonce : _nonce},
security: '<?php echo wp_create_nonce( "a" ); ?>',
dataType: 'json'
});
request.done(function (response, textStatus, jqXHR){
str = JSON.stringify(response);
if(response["error"]){
alert(response["message"]);
}
nonce 被添加到页面中,代码如下:
$nonce = wp_create_nonce( 'a' );
echo "<input type='hidden' id='_nonce' value=" . $nonce ."/>";
在 php 中,以下函数片段用于获取和比较 nonce:
function ace($request) {
global $wpdb;
$timestamp = time();
$nonce = (string) $request['_nonce'];
$verify = check_ajax_referer( 'a', $nonce, false );
if ( ! $verify){
$errMsg = $nonce . '-'. $verify .' not validated ';
return (object) array(error => true, message => $errMsg);
}
我总是收到“未验证”的消息。 $nonce 有一个值,但 $verify 永远不会得到一个值。
【问题讨论】:
标签: php ajax wordpress rest nonce