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

Implementing Cross-Domain AJAX (GET and POST) in ShopNC After Setting $config[‘wap_site_url’]=’http://mall.m.ygt.cm’;: A Workflow for Handling Cookies in Request Headers

Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 2.6

1. The request code for $.getJSON is shown in Figure 1.0.

The request code for $.getJSON is shown in Figure 1.0
The request code for $.getJSON is shown in Figure 1.0

2. Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 1.1.

Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 1.1
Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 1.1

3. Add &callback=? to the request URL in $.getJSON, which is equivalent to setting the request format to jsonp, as shown in Figure 1.2.

Add &callback=? to the request URL in $.getJSON, which is equivalent to setting the request format to jsonp, as shown in Figure 1.2
Add &callback=? to the request URL in $.getJSON, which is equivalent to setting the request format to jsonp, as shown in Figure 1.2

4. Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 1.3.

Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 1.3
Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 1.3

5. The POST request under $.ajax, with the code shown in Figure 2.0.

The POST request under $.ajax, with the code shown in Figure 2.0
The POST request under $.ajax, with the code shown in Figure 2.0

6. Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 2.1.

Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 2.1
Inspecting the network, I found that a new PHPSESSID was generated in the response Cookie, as shown in Figure 2.1

7. JS code implementation: dataType:’jsonp’,crossDomain: true, as shown in Figure 2.2.

JS code implementation: dataType:'jsonp',crossDomain: true, as shown in Figure 2.2
JS code implementation: dataType:’jsonp’,crossDomain: true, as shown in Figure 2.2

8. Inspecting the network, I found that the request Cookie was already consistent with the current URL, but the POST request had been converted to a GET request, as shown in Figure 2.3.

Inspecting the network, I found that the request Cookie was already consistent with the current URL, but the POST request had been converted to a GET request, as shown in Figure 2.3
Inspecting the network, I found that the request Cookie was already consistent with the current URL, but the POST request had been converted to a GET request, as shown in Figure 2.3

9. JS code implementation: dataType:’json’,xhrFields:{withCredentials:true},crossDomain: true, as shown in Figure 2.4.

JS code implementation: dataType:'json',xhrFields:{withCredentials:true},crossDomain: true, as shown in Figure 2.4
JS code implementation: dataType:’json’,xhrFields:{withCredentials:true},crossDomain: true, as shown in Figure 2.4

10. PHP code implementation:
header(“Access-Control-Allow-Credentials: true”);
header(“Access-Control-Allow-Origin: ” . C(‘wap_site_url’) . “”);

Where the value of C(‘wap_site_url’) is: http://mall.m.ygt.cm, as shown in Figure 2.5.

PHP code implementation: header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Origin: " . C('wap_site_url') . ""); Where the value of C('wap_site_url') is: http://mall.m.ygt.cm, as shown in Figure 2.5
PHP code implementation:
header(“Access-Control-Allow-Credentials: true”);
header(“Access-Control-Allow-Origin: ” . C(‘wap_site_url’) . “”);
Where the value of C(‘wap_site_url’) is: http://mall.m.ygt.cm, as shown in Figure 2.5

11. Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 2.6.

Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 2.6
Inspecting the network, I found that the request Cookie was already consistent with the current URL, as shown in Figure 2.6

12. JS code implementation for document.cookie: domain=.ygt.cm; as shown in Figure 2.7.

JS code implementation for document.cookie: domain=.ygt.cm; as shown in Figure 2.7
JS code implementation for document.cookie: domain=.ygt.cm; as shown in Figure 2.7

13. The POST request under $.ajax, with the code shown in Figure 3.0, using the jsonp format; in fact, you can also adopt the same handling approach as for the POST request under $.ajax, using the json format and then setting xhrFields.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系