jQuery 文档 jQuery.post( )
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
Run Code Online (Sandbox Code Playgroud)
success:参数和jqXHR.done( )方法有什么区别; 如果没有,那么该jqXHR.done( …
我有一个$ .ajax()请求,其dataType设置为"json".服务器使用正确的mime类型"application/json"返回JSON.然而,我的jqXHR对象中的responseText始终是一个字符串.我究竟做错了什么?这是它应该如何工作?
这是我打电话的方式:
var options = {
dataType:'json',
type: 'GET',
url: "http://example.com/api/"
};
var key = "PassToCallback";
var jqXHRObject = $.ajax(options).then(
function(data, textStatus, jqXHR, key) {
this.success(data, textStatus, jqXHR, key);
},
function(jqXHR, textStatus, errorThrown) {
this.error(jqXHR, textStatus, errorThrown);
}
);
console.log(jqXHRObject.getResponseHeader("content-type")); // application/json
console.log(typeof jqXHRObject.responseText); // string
Run Code Online (Sandbox Code Playgroud)
所以我必须做一个$.parseJSON(jqXHRObject.responseText)获得一个实际的对象.这似乎是不必要的,因为$ .ajax()应该根据文档自动转换responseText.谢谢!
我正在尝试使用XMLHttpRequest在XSS请求中设置Cookie.
我找到了XMLHttpRequest规范,4.6.2-5节似乎确实建议不允许设置Cookie,Cookie2和其他一些标题,但我希望有一个解决方法.
我的(jQuery)代码如下所示,但由于未设置cookie,因此生成的查询失败.
$.ajax( {
type : "POST",
url : URL,
data: SOAP_INBOX_MAIL_QUERY,
dataType : "xml",
async: false,
beforeSend : function(xhr) {
var cookie = credentials["COOKIE"];
console.info( "adding cookie: "+ cookie );
xhr.setRequestHeader('Cookie', cookie);
},
success : function(data, textStatus, xmLHttpRequest){
},
error : function(xhr, ajaxOptions, thrownError) {
credentials = null;
}
});
Run Code Online (Sandbox Code Playgroud) jqXHR = $.ajax({ url: $frm.attr("action"), type: "POST", dataType: "json", cache: false,
headers: headers, contentType: "application/json;charset=UTF-8", data: ko.mapping.toJSON(data, map),
beforeSend: function(x) {
if (x && x.overrideMimeType) {
return x.overrideMimeType("application/json;charset=UTF-8");
}
}
});
jqXHR.fail(function(xhr, err, msg) { /* xhr.responseText NEED TO BE JSON!!! */ });
Run Code Online (Sandbox Code Playgroud)
头
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4
Connection:keep-alive
Content-Length:10
Content-Type:application/json;charset=UTF-8
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source {Id:0} …Run Code Online (Sandbox Code Playgroud) 触发AJAX GET以http://qualifiedlocalhost:8888/resource.json启动预期的CORS预飞行,看起来它正确回归:
OPTIONS请求Request URL:http://qualifiedlocalhost:8888/resource.json
Request Method:OPTIONS
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)
请求标题
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, x-requested-with
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:qualifiedlocalhost:8888
Origin:http://localhost:9000
Pragma:no-cache
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36
Run Code Online (Sandbox Code Playgroud)
响应标题
Access-Control-Allow-Headers:Content-Type, X-Requested-With
Access-Control-Allow-Methods:GET,PUT,POST,DELETE
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:2
Content-Type:text/plain
Date:Thu, 01 Aug 2013 19:57:43 GMT
Set-Cookie:connect.sid=s%3AEpPytDm3Dk3H9V4J9y6_y-Nq.Rs572s475TpGhCP%2FK%2B2maKV6zYD%2FUg425zPDKHwoQ6s; Path=/; HttpOnly
X-Powered-By:Express
Run Code Online (Sandbox Code Playgroud)
它应该工作,对吗?
但随后的请求仍然失败并出现错误 XMLHttpRequest cannot load http://qualifiedlocalhost:8888/resource.json. Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin.
Request URL:http://qualifiedlocalhost:8888/resource.json
Run Code Online (Sandbox Code Playgroud)
请求标题
Accept:application/json, text/plain, */*
Cache-Control:no-cache
Origin:http://localhost:9000 …Run Code Online (Sandbox Code Playgroud) 我正在开发基于Java,JSP,Ajax的Web应用程序,服务器是JBoss,前面是Apche 2服务器.该应用程序通过互联网访问.客户主要使用IE 7,8,9浏览器.
应用程序运行良好之前.最近我们在网站上应用了SSL证书,之后我们开始收到用户抱怨应用Ajax的页面没有提交.通常我们在提交按钮上调用ajax并阻止页面直到从服务器收到响应.我们应用了记录器,发现ajax请求调用失败,ajax jqXHR错误状态为12019.
我们观察到使用IE(主要是版本8和9)的用户观察到此错误.错误的模式也不一样.有一段时间,同一个用户能够提交早期投诉的页面,有些时间没有.我最好在我的本地和开发环境中生成这个场景,但不能.
我谷歌问题,发现12019错误代码是IE的错误.但为什么它在应用SSL之前有效,
如果SSL不正确,为什么问题只出现在ajax页面上,为什么不在其他地方.我不能说用户改变浏览器.
这让我疯狂了一个多星期.如果其他人已经观察并解决过这个问题,请帮助我.
我有一个ajax请求,我故意从我的服务器端代码失败来触发错误处理事件.我想知道是否有可能获得它尝试的URL?我想获取该URL并将其注入超链接并重试该请求.
这可能吗?
编辑
我能够看到尝试的URL请求通过FireBug和检查jqxhr对象,console.dir()并且似乎无法找到任何有助于我识别它试图调用的URL的内容.理想情况下,不希望存储全局变量,希望从参数中获取此信息.
在此先感谢,O.
$.ajax({
type: 'get',
url: 'somewhere/foo',
context: this,
success: this.mySuccess,
error: this.myError,
cache: false
});
myError = function (jqXhr, textStatus) {
alert(jqXhr.url); //Get url of failed request and inject it into hyper link?
};
Run Code Online (Sandbox Code Playgroud) 当对xhr请求的响应是301时,有没有办法获取最终重定向到的URL?
我的网站包含来自旧版本的大量遗留网址,这些网址会返回301对正确新网址的回复.
出于实用目的,我希望能够向旧URL发出请求,并能够检索新的URL,即发送请求到"/oldpage.aspx?foo=someParam",获取新的URL"/ arbitaryNewPageName/someParam".
我在firebug控制台中一直在玩这个:
$.ajax({
url: "/oldpage.aspx?foo=someParam",
success: function(response, status, jqxhr){
//poking around, trying to get the new URL, "/arbitraryNewPage/someParam"
console.log(jqxhr.getAllResponseHeaders());
console.log(jqxhr);
},
beforeSend: function(jqxhr, settings){
console.log(jqxhr);
console.log(settings);
}
});
Run Code Online (Sandbox Code Playgroud)
我可以在firebug中看到,当这段代码运行时,它会对"/oldpage.aspx?foo=someParam"执行一次GET,并获得301响应,然后获得另一个GET到"/ arbitaryNewPageName/someParam".
对于第一个请求,我在响应标头的Location值中看到重定向的URL.不幸的是,第二个请求是传递给$ .ajax.success函数的内容,它只在请求头的Referrer值中有重定向的URL.
是否有可能拦截对第一个响应的响应,或者可能会看到第二个请求的请求标头?
编辑:感谢大家的回复.我想我需要提供一些背景来澄清我到底在寻找什么.
业务用户要求我创建一个将旧URL与新URL相关联的列表.由于我已经实现了将旧URL重定向到服务器上的新URL的方法,因此我希望能够减少这项工作,并创建一个脚本,将请求放置到旧URL并获取请求被重定向到的URL.像这样的东西:
for (var i = 0; i < arrayOfLegacyUrls.length; i++)
{
$.ajax({
url: arrayOfLegacyUrls[i],
success: function(response, status, jqxhr){
var newUrl = "???"; //do magic to get URL that request was redirected to
writeToFileForBusinessUser(arrayOfLegacyUrls[i], newUrl);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的问题的关键在于:我可以从XHR获取我的请求被重定向到的URL吗?到目前为止,答案似乎是"不".
我正在尝试进行跨域请求,我的服务器配置为发送以下标头:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with, Authorization
Access-Control-Allow-Methods:OPTIONS, GET, HEAD, POST
Access-Control-Allow-Origin:*
Run Code Online (Sandbox Code Playgroud)
但是当发出OPTION请求时,我会收到OPTIONS 405 (Method Not Allowed)错误.
任何想法是什么问题以及如何解决它?
此代码使用Microsoft Web Api Http堆栈和jQuery.
如何获取由jQuery的deferred.fail()显示的由HttpError参数创建的自定义错误消息给CreateErrorResponse ()?
在ApiController中为测试目的创建错误响应的示例:
public HttpResponseMessage Post(Region region)
{
var error = new HttpError("Failure to lunch.");
return this.Request.CreateErrorResponse(
HttpStatusCode.InternalServerError,
error);
}
Run Code Online (Sandbox Code Playgroud)
这是一个减少客户端,试图找到要显示的错误消息,"未能午餐.".
$.ajax({
type: 'POST',
url: 'api/region',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(region)
})
.fail(function (jqXhr, textStatus, errorThrown) {
alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText);
});
Run Code Online (Sandbox Code Playgroud)
将显示的是:
"错误:内部服务器错误:{full stack here}"
我想要的是:
"没有吃午饭."
jqxhr ×10
jquery ×6
ajax ×5
javascript ×4
cors ×2
c# ×1
cross-domain ×1
express ×1
get ×1
httpresponse ×1
jquery-1.5 ×1
json ×1
node.js ×1
sencha-touch ×1
ssl ×1