dsvbtgo639708 2014-01-23 20:44 采纳率: 0%
浏览 69
已采纳

通过Ajax发送flash消息(使用cookie好/坏做法?)

I'd like to send a flash message to show when a request finished. I'd like to put the message directly in PHP, because the logic of which flash message should in my opinion be in the PHP.

My problem is that I send Json data, for example when you GET users/

The JS receives:

[{user:"John","connected":true},{user:"Jane","connected":false},...] // =data

I would like to send the flash Message inside the response, but I don't want to change the Json Output (because it becomes less Developper friendly to send a custom object like {"flash_message":"You are connected","data":data})

So I thought about setting some Cookies that expire in 1 minute, so I can show the flash message when the request is received.

Is this good practice ?

  • 写回答

1条回答 默认 最新

  • duanlu6268 2014-01-28 13:40
    关注

    Using cookies is not the best way to go here, because cookies are intended to be persistent data storages, not to send data on a request.

    Custom requests headers are much more appropriated.

    In PHP you can send them like this:

    header("X-flash-message-content: $message");
    

    In JS you can retrieve them like this

    var req = new XMLHttpRequest();
    req.open('GET', document.location, false);
    req.send(null);
    var headers = req.getAllResponseHeaders().toLowerCase();
    console.log(headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺