发送一个简单的 GET 请求

发布于 2024-09-08 20:17:20 字数 815 浏览 1 评论 0原文

我想发送一个服务器简单的 GET 请求,但正如我看到 .ajax 发送的 x-requested-with 标头,我的服务器无法理解。

                    $.ajax({
                        type: 'GET',
                        url: 'coord-' + x + '-' + y + '-' + iname,
                        success: function(data) {
                        $('img').each(function(idx, item) {
                            var img_attr = $(this).attr("src")
                            var name = img_attr.match(/\d+-\d+\.\w+/)
                            name = name + '?r=' + Math.random()
                            $(this).removeAttr("src")
                            $(this).attr("src",name)
                        })
                    }, 
                    })

在标题中 ---> X-Requested-With:XMLHttpRequest
是否可以在不使用 x-request-with 的情况下发送简单的请求?

I want to send a server simple GET request but as I see the .ajax sends x-requested-with header which my server doesn't understand.

                    $.ajax({
                        type: 'GET',
                        url: 'coord-' + x + '-' + y + '-' + iname,
                        success: function(data) {
                        $('img').each(function(idx, item) {
                            var img_attr = $(this).attr("src")
                            var name = img_attr.match(/\d+-\d+\.\w+/)
                            name = name + '?r=' + Math.random()
                            $(this).removeAttr("src")
                            $(this).attr("src",name)
                        })
                    }, 
                    })

in headers ---> X-Requested-With: XMLHttpRequest
Is it possible to send a simple request without using x-request-with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-09-15 20:17:20

这看起来像您需要设置 contentType 参数,如下所示:

                     $.ajax({               
                        type: 'GET',
                        url: 'coord-' + x + '-' + y + '-' + iname,
                        contentType: 'application/json; charset=utf-8'
                        success: function(data) {....

                         beforeSend: function(xhr) {
                                xhr.setRequestHeader("Content-type", 
                         "application/json; charset=utf-8");
                         },

Encosia 在 .net 环境中有一篇关于此的好文章。

This looks like you need to set the contentType parameter, something like this:

                     $.ajax({               
                        type: 'GET',
                        url: 'coord-' + x + '-' + y + '-' + iname,
                        contentType: 'application/json; charset=utf-8'
                        success: function(data) {....

or

                         beforeSend: function(xhr) {
                                xhr.setRequestHeader("Content-type", 
                         "application/json; charset=utf-8");
                         },

Encosia has a good post about this from the .net enviroment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文