没有不值得去解决的问题,也没有不值得去学习的技术!

How to Troubleshoot HTTP 500 Responses Caused by Setting the src Attribute With the jQuery attr() Method

Open the Initiator tab to check the request call stack. The GET request is executed at line 516 of the page.

1. After the image is uploaded successfully, the GET request to https://console.xxx.cn/upload/20210319/20210385015127.png returns a 500 response. The root cause is that the domain for the uploaded image is not https://console.xxx.cn, but rather https://web.xxx.cn. See Figure 1.

After the image is uploaded successfully, the GET request to https://console.xxx.cn/upload/20210319/20210385015127.png returns a 500 response. The root cause is that the domain for the uploaded image is not https://console.xxx.cn, but rather https://web.xxx.cn.
Figure 1

2. Open the Initiator tab to check the request call stack. The GET request is executed at line 516 of the page. See Figure 2.

Open the Initiator tab to check the request call stack. The GET request is executed at line 516 of the page.
Figure 2

3. Click to view the JS code. The jQuery attribute operation – attr() method sets the src attribute. See Figure 3.

Click to view the JS code. The jQuery attribute operation - attr() method sets the src attribute.
Figure 3
JavaScript

goodsSl.on('uploadSuccess', function (file, json) {
    $('.edit_img_tipthumb').attr("src", json.theUrl);
    $('input[name="data[Product][thumb_img]"]').val(json.theUrl);
})

4. The value of json.theUrl is /upload/20210319/20210385015127.png. I decided to use an absolute address by adding the domain prefix: https://web.xxx.cn. The edited code is as follows:

JavaScript

goodsSl.on('uploadSuccess', function (file, json) {
    $('.edit_img_tipthumb').attr("src", 'https://web.xxx.cn/creditshop_back' +  json.theUrl);
    $('input[name="data[Product][thumb_img]"]').val(json.theUrl);
})

5. After the image is uploaded successfully, the GET request to https://web.xxx.cn/upload/20210319/20210385015127.png returns a 200 response. See Figure 3.

After the image is uploaded successfully, the GET request to https://web.xxx.cn/upload/20210319/20210385015127.png returns a 200 response.
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系