【发布时间】:2014-10-04 23:07:46
【问题描述】:
我正在尝试使用 mcrypt 并加密和解密跨域的 json 数据。实际上,当我在同一个域中使用加密时,这有效,但在 ajax 中无效。
Here is function all:
function all($contentType = 'page') {
$secret_key = "12345678911234567892123456789312";
$this->load->database();
$query = $this->db->query(
"SELECT * FROM category"
);
$buffer = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, json_encode($result), MCRYPT_MODE_ECB);
$this->output->set_output(base64_encode($buffer));
Here is file of view:
$(function(){
try {
$.ajax({
type: "GET",
url: "http://localhost:8888/DIY/index.php/user/all/json",
crossDomain: true,
contentType: "application/x-www-form-urlencoded",
async: false,
dataType: 'text',
processData: false,
cache: false,
success: function (response) {
var a = mcrypt.Decrypt($.base64('decode', response ));
console.log( JSON.parse(a) );
},
error: function (ErrorResponse) {
console.log('error');
}
});
}
catch (error) {
}
});
The output in Chrome is:
[{"id":"1","EMAIL":"sda@q.com","PHONE":"sdsng","USERNAME":"12das","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null},{"id":"2","EMAIL":"adas","PHONE":"dsada","USERNAME":"asdasd","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null}]
如果我在视图文件中添加 JSON.parse(a),它不会显示任何内容。但是,我在Chrome中得到console.log(a)后,直接将输出数据复制为参数,在Chrome控制台中使用
JSON.parse('[{"id":"1","EMAIL":"139520519@qq.com","PHONE":"yang_zhang","USERNAME":"122545929","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null},{"id":"2","EMAIL":"adas","PHONE":"dsada","USERNAME":"asdasd","FIRST_NAME":null,"LAST_NAME":null,"PASSWORD":"","TYPE":null}]')
[Object, Object]
输出就是我要找的!!!谁能帮我解决这个问题?
【问题讨论】:
标签: ajax json codeigniter base64 mcrypt