使用 jQuery 1.5 以 jsonp 形式发送请求,将响应解释为文本

发布于 2025-01-04 17:53:21 字数 1434 浏览 3 评论 0原文

简短的问题:有没有办法向服务器发出 jsonp 请求,捕获请求,但不将其解析为 javascript?我在 jQuery 1.5 中使用 dataType: "jsonp text" 但它不起作用。

我正在尝试使用 jsonp 通过 AJAX 访问跨域 URL。问题是另一个域(我大学的目录列表)非常旧,我怀疑服务器是否支持 jsonp。

  • 在 Firefox 中,我收到“XML 标记名称不匹配(预期 META)”错误。在 chrome 中,我收到“Uncaught SyntaxError Unexpected token <”两者都指向与我的 AJAX 请求相对应的文件。错误回调中的错误字符串是“parsererror”。
  • 我无法进行正常的 AJAX 调用 - 当我将数据类型更改为“文本”或将其全部删除时,另一个域会抱怨用户未经过身份验证并重定向到登录页面 - 即使我已经登录浏览器端。当 dataType 为 jsonp 时,不会发生这种情况。
  • 我知道服务器需要支持 JSONP,而且我认为不需要,但是当我将 dataType 更改为 JSONP 时,我可以看到响应页面资源显示在 Chrome 和 Firefox 中 - 所以服务器实际上将响应发送到浏览器(一个静态 HTML 网页 + 一些 java 脚本)——其中包含我想要获取的数据。
  • 问题是 jQuery 试图将响应解析为 javascript,但失败了(因为它不是 javascript)。所以数据最终会出现在浏览器中——我只需要访问它!
  • 使用 dataType: "jsonp text" 应该指示发送 jsonp 请求并将响应解释为文本没有任何区别 - 仍然是解析错误。

我想要的是:一种以纯文本形式访问 jsonp 请求响应的方法。或者,如果我可以访问失败的 jsonp 请求的原始响应——那也可以。

提前致谢!

代码:

ajax_url = 'https://somesite/?searchTerm='+query+'&searchType=lastname';
var jqxhr = $.ajax({type:"GET",
url: ajax_url,
dataType:"jsonp text",
callback: "whatever",
success:function(responseData) {
   $('div#content').text( responseData.slice(0, 100) );
   dbg(responseData.slice(0,100));
}})
.success(function() { alert("success"); })
.error(function(obj, errStr) { alert("error"); dbg("error: " + errStr + "test: " + test.responseText + this.responseTxt);})
.complete(function() { alert("complete"); });

Short question: is there a way to make a jsonp request to a server, capture the request, but not parse it as javascript? I am using dataType: "jsonp text" in jQuery 1.5 but it is not working.

I'm trying access a cross-domain URL via AJAX with jsonp. The problem is that the other domain (a directory listing at my university) is very old and I doubt the server supports jsonp.

  • In Firefox, I get a "XML tag name mismatch (expected META)" error. In chrome I get a "Uncaught SyntaxError Unexpected token <" both pointing to a file corresponding to my AJAX request . The error string from the error callback is "parsererror".
  • I can't do a normal AJAX call -- when I change datatype to just "text" or remove it all together, the other domain complains that the user is not authenticated and redirects to a login page -- even if I've already logged in browser side. When dataType is jsonp, this doesn't happen.
  • I know the server needs to support JSONP, and I don't think it does, but when I change dataType to JSONP, I can see the response page resources show up in both Chrome and Firefox -- so the server actually sends the response to the browser (a static HTML webpage + some java script) -- which contains the data I want to get at.
  • The problem is that jQuery is trying to parse the response as javascript, and fails (because it's not javascript). So the data ends up in the browser -- I just need to access it!
  • Using dataType: "jsonp text" which is supposed to indicate send a jsonp request and interpret the response as text makes no difference -- still a parse error.

What I want is: a way to access the response from a jsonp request as plain text. Or, if I can access the raw response from a failed jsonp request -- that would work too.

Thanks in advance!

Code:

ajax_url = 'https://somesite/?searchTerm='+query+'&searchType=lastname';
var jqxhr = $.ajax({type:"GET",
url: ajax_url,
dataType:"jsonp text",
callback: "whatever",
success:function(responseData) {
   $('div#content').text( responseData.slice(0, 100) );
   dbg(responseData.slice(0,100));
}})
.success(function() { alert("success"); })
.error(function(obj, errStr) { alert("error"); dbg("error: " + errStr + "test: " + test.responseText + this.responseTxt);})
.complete(function() { alert("complete"); });

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

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

发布评论

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

评论(1

海拔太高太耀眼 2025-01-11 17:53:21

你想要实现的目标是行不通的:只有当服务器将响应正确嵌入到 JavaScript 函数调用中时,才能使用 jsonp。

即使服务器确实将文件发送到浏览器,由于安全限制,您将无法访问它:您所能做的就是将所述数据作为脚本(带有脚本标签)执行或将其用作样式表(使用链接标签),但如果它来自另一个域,您将永远无法检查原始文本中的响应。

我想您尝试直接获取 xml 格式的数据,但失败了(意味着该网站不支持 CORS) 。如果您无法更改服务器端代码,那么您只有两种选择:

  1. 在您自己的服务器上创建一个代理来隧道响应(您的服务器端脚本发出实际请求并将其发送到浏览器): Same-domain 将然后从客户端的角度应用,您将能够以 XML 形式请求数据,
  2. 使用 flash 绕过浏览器的安全性(您可以使用 flXHR 但似乎有点过时了 现在)。

What you're trying to achieve won't work: jsonp can only be used if and when the server properly embed the response in a javascript function call.

Even though the server does send the file to the browser, you won't be able to access it because of security limitations: all you can do is execute said data as a script (with a script tag) or use it as a stylesheet (using a link tag) but you'll never, ever, be able to inspect the response in raw text if it's from another domain.

I suppose you tried to get the data as xml directly and that it failed (meaning the site does not support CORS). If you cannot change code server-side, then you only have two alternatives:

  1. Create a proxy on your own server that would tunnel the response (your server-side script makes the actual request and sends it to the browser): same-domain will then apply from the client's point of view and you'll be able to request data as XML,
  2. Use flash to bypass browser's security (you can use flXHR but it seems to be a little dated now).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文