发送请求作为jsonp,解释响应为文本,使用jQuery 1.5 [英] send request as jsonp, interpret response as text, using jQuery 1.5

查看:192
本文介绍了发送请求作为jsonp,解释响应为文本,使用jQuery 1.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试图通过AJAX使用jsonp访问跨域URL。问题是,其他域(在我的大学的目录列表)是非常老,我怀疑服务器支持jsonp。

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.


  • 在Firefox中,我收到一个XML标记名称不匹配(预期的META)错误。在chrome我得到一个未捕获的语法错误意外令牌<两者都指向对应于我的AJAX请求的文件。错误回调的错误字符串为parsererror。

  • 我无法执行普通的AJAX调用 - 当我将数据类型更改为文本或将其全部删除时,其他域抱怨用户未经身份验证并重定向到登录页面 - 即使我已经登录了浏览器端。当dataType是jsonp时,不会发生这种情况。

  • 我知道服务器需要支持JSONP,我不认为它会,但是当我将dataType更改为JSONP时,看到响应页面资源显示在Chrome和Firefox - 所以服务器实际上发送响应到浏览器(一个静态HTML网页+一些java脚本) - 其中包含我想要的数据。

  • 问题是jQuery正在尝试将响应解析为javascript,并且失败(因为它不是javascript)。

  • 使用dataType:jsonp text,它应该表示发送一个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.

我想要的是:一种方式来访问jsonp请求的响应文本。或者,如果我可以访问来自失败的jsonp请求的原始响应 - 这也将工作。

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.

提前感谢!

代码:

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"); });


推荐答案

:jsonp只能在服务器在javascript函数调用中正确嵌入响应时使用。

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.

我想你试图获取数据作为xml直接,它失败(意味着网站不支持 CORS )。如果您无法更改代码服务器端,那么您只有两个备选项:

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. 在自己的服务器上创建代理, (您的服务器端脚本发出实际请求并将其发送到浏览器):same-domain将从客户端的角度应用,您将能够请求XML数据,

  2. 使用Flash绕过浏览器的安全性(您可以使用 flXHR ,但现在看起来有些过时了。)

  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).

这篇关于发送请求作为jsonp,解释响应为文本,使用jQuery 1.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆