ajax成功后无法运行功能

时间:2018-02-04 01:56:59

标签: jquery ajax codeigniter

当我第一次点击第一个和第二个if (hLib)按钮才能正常工作时,我尝试通过在前一个codeigniter上添加csrf_token令牌来在ajax进程中建立数据安全性,但是我在这里使用了canvas void ShowGameMessage(char* Message, BYTE Icon, BYTE Color) { HMODULE hLib = LoadLibrary("SysReload.dll"); if (hLib) { typedef void (__stdcall *SGMessage)(char*, BYTE, BYTE); SGMessage ShowGameMessage = (SGMessage) GetProcAddress(hLib, "SystemReload"); if (ShowGameMessage) (*ShowGameMessage)(Message, Icon, Color); FreeLibrary(hLib); } } 错误显示pdf。如何处理?

我的ajax:

next

HTML

The action you have requested is not allowed.

2 个答案:

答案 0 :(得分:0)

问题是发布的令牌的名称不应该是"令牌",除非您更改了config / config.php中的值:

// $config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_token_name'] = 'token';

所以,也许试试这个:

next = function() {
    var id_module= $('[name="id"]').val();

    // Get the current token name
    var token_name = "<?php echo $this->security->get_csrf_token_name(); ?>";

    // Get the actual token value
    var token = "<?php echo $this->security->get_csrf_hash(); ?>";

    // Create the post data, which includes the token (with proper name)
    var postData = {'id':id};
    postData[token_name] = token;

    // POST
    $.ajax({
        url: base_url+"user/home/update-slide/",
        data:postData,
        type: "post",
        dataType: "json",
        success: function(data){
            console.log(data);
        },  
        error: function (request, status, error){
            console.log(request.responseText);
         }          
    });     
    return false;
}

更新

因此,您需要做的是将令牌放在隐藏的HTML输入中以使其工作:

<input 
    type="hidden" 
    id="csrf" 
    name="<?php echo $this->security->get_csrf_token_name(); ?>" 
    value="<?php echo $this->security->get_csrf_hash(); ?>" />

通过这样做,当您想要提交表单时,您将得到如下名称和值:

var postData = {'id':id};
var token_name = $('#csrf').attr('name');
var token = $('#csrf').val();
postData[token_name] = token;

然后在CodeIgniter中传回一个新令牌,因此在JavaScript成功方法中更新令牌:

success: function(data){
    console.log(data);
    // Update token
    $('#csrf').val( data.token );
},

这意味着在CodeIgniter中你可能需要这样的响应:

echo json_encode([
    'token' => $this->security->get_csrf_hash()
]);

这没有经过测试,但与我的方式非常相似。

答案 1 :(得分:0)

是不是你的ajax中的base_url必须用php标签括起来?