duandui2803 2018-08-16 11:35
浏览 31

Symfony 4 /如何使用Ajax正确更新twig资产客户端,而不依赖于当前的url?

I have the following template code on my homepage example.com

{% block content %}

  <img id='imgID' src={{ asset(/asset/img.png) }}>

{% endblock %}

This loads correctly

Now I navigate to example.com/foo by clicking on a link. This page contains an Ajax request that returns a new src for my <img> in the promise:

$('#imgID').attr('src', '/asset/${newimg}')

The image updates correctly BUT now if I refresh the browser page by clicking on the browser refresh button I get 404 on my image

Error Failed to load resource: the server responded with a status of 404 (Not Found) http://example.com/foo/asset/newimg.png'

it should be http://example.com/asset/newimg.png'

same error if I load the ajax call from anything other than example.com

I do not understand why, I am using an absolute path in my Ajax callback. The twig {{ asset() }} function is not loading correctly?

What is the correct way of updating a twig asset path with an ajax request from any url without having to use the asset manager server side? Can I somehow specify a true absolute path ?

Do I have to use composer require symfony/asset like below and therefore make an Ajax call every time I want to update an asset with Ajax ? Or can I keep it client side ? the entire thing seems over complicated and a complete mess

server code:

$package = new Package(new EmptyVersionStrategy());
$img = $package->getUrl('/asset/newimg.png');
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入