确定AJAX调用成功的正确方法是什么?
我在prototype.js中看到了
return !status || (status >= 200 && status < 300);
Run Code Online (Sandbox Code Playgroud)
在jQuery中:
// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
return !xhr.status && location.protocol == "file:" ||
( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;
Run Code Online (Sandbox Code Playgroud)
哪一个是正确的?如果我们不使用任何Javascript库但是在基本Javascript中编写AJAX,我们应该使用哪一个?
我想通过java填充HTTP表单的文本字段,然后想通过java单击提交按钮,以获取提交表单后返回的文档的页面源.我可以通过直接发送HTTP请求来做到这一点,但我不这样做.
我正在开发一个发布到网站的应用程序,我正在尝试将实体响应存储为字符串.但是,字符串似乎只包含一小部分响应,大约35行左右.我想知道它是否与缓冲区溢出有关但我真的不确定.我的代码如下:
static String getResponseBody(HttpResponse response) throws IllegalStateException, IOException{
String content = null;
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
{
if(isBlankString(line) == false)
{
sb.append(line + "\n");
}
}
br.close();
content = sb.toString();
}
return content;
Run Code Online (Sandbox Code Playgroud)
isBlankString只是注意一条线是否包含任何字符,因为响应中有很多空白行让我烦恼.我有一个问题,即无论有没有得到整个回应.任何人都知道发生了什么或如何解决这个问题?
谢谢
我有Web应用程序(在Java中),在这里我需要通过jquery或简单的javaScript ajax调用在任何浏览器(包括IE限制XDomainRequest对象)中通过Ajax调用来限制跨域请求。
我的最终目的是在通过某些浏览器设置或设置响应标头进行调用时限制它,以使它不会在第一点本身上进行调用。
如果解决方案是同等政策,请务必说明如何解决。
感谢和问候,Oceanvijai
我花了很多时间研究使用Cocoa的POST请求.
我发现一些看起来不错的代码.我改变它就像它符合我的需要.但代码不起作用,我找不到bug,因为我对可可很新.
这是我的代码.
- (IBAction)sendForm:(id)sender
{
NSLog(@"web request started");
NSString *post = [NSString stringWithFormat:@"firstName=%@&lastName=%@&eMail=%@&message=%@", firstName.stringValue, lastName.stringValue, eMail.stringValue, message.stringValue];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"myDomain/form.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection)
{
webData = [[NSMutableData data] retain];
NSLog(@"connection initiated");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"connection received data"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试理解当我使用Requests模块发出POST请求时我到底得到了什么 - 它总是JSON吗?似乎我得到的每个响应似乎都是JSON,但我不确定.
r当我这样做时,我的回复对象在哪里:
print r.apparent_encoding
Run Code Online (Sandbox Code Playgroud)
它似乎总是回归 ascii
当我尝试type():
>>>print type(r)
<class 'requests.models.Response'
Run Code Online (Sandbox Code Playgroud)
我将输出粘贴print r.text到JSON验证器中,并且报告没有错误.那么我应该假设Requests在这里提供我的JSON对象吗?
我对CORS服务器进行AJAX调用,我尝试使用javascript客户端读取响应时返回的cookie,但是徒劳无功.
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Set-Cookie');
response.header('Access-Control-Expose-Headers', "Set-Cookie");
//------SET COOKIES
response.cookie('SessionId', GeneratorId(64), {
maxAge:3600000,
httpOnly:flase // i disable httpOnly ==> does not work
});
Run Code Online (Sandbox Code Playgroud)
var xhttp=new XMLHttpRequest();
xhttp.open("POST", "http://localhost:9090/api/map", true);
xhttp,send(`{layer:1}`);
Run Code Online (Sandbox Code Playgroud)
//Append another response' header
response.header('Access-Control-Allow-Credentials','true');
Run Code Online (Sandbox Code Playgroud)
// Before xhttp.send , I add another instruction :
xhttp.withCredentials=true;
Run Code Online (Sandbox Code Playgroud)
//Avoid the wildcard on Access-Control-Allow-Origin =>Replace the first header by :
response.header('Access-Control-Allow-Origin', request.get('Origin'));
Run Code Online (Sandbox Code Playgroud)
尽管我检查了一个恶意URL(http://fileserver03.com),但仍从返回空响应Google Safe Browsing API v4。
这是我尝试过的代码:
String postURL = https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY
String requestBody = "{" +
" \"client\": {" +
" \"clientId\": \"twittersentidetector\"," +
" \"clientVersion\": \"1.0\"" +
" }," +
" \"threatInfo\": {" +
" \"threatTypes\": [\"MALWARE\", \"SOCIAL_ENGINEERING\"]," +
" \"platformTypes\": [\"ANY_PLATFORM\"]," +
" \"threatEntryTypes\": [\"URL\"]," +
" \"threatEntries\": [" +
" {\"url\": \"http://fileserver03.com\"}," +
" {\"url\": \"https://bing.com\"}," +
" {\"url\": \"https://yahoo.com\"}" +
" ]" +
" }" +
" }";
URL url = new URL(postURL); …Run Code Online (Sandbox Code Playgroud) 在调用REST服务时,HttpClientErrorException我可以获取状态代码和错误消息,但如何获得responseBody?
我正在尝试以下代码,但是无法强制转换为HttpResponse。
catch(HttpClientErrorException e) {
// I am trying to typecast to HttpResponse, but its throwing error
HttpResponse response = (HttpResponse) e;
String msg = response.getEntity().getContent().toString();
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?有人可以建议吗?
我正在使用Drop-wizard jersey客户端,该客户端内部使用apache http客户端,在我的情况下,如果服务器端发生任何错误,即它在5xx Server错误中返回状态代码。
我使用下面的代码检查服务器是否返回5XX代码。
if (response.getStatus() / 100 == 5) {
// do some work
}
Run Code Online (Sandbox Code Playgroud)
javax.ws.rs.core.Response类中也有类似的代码,从那里复制以下代码:-
public static Family familyOf(final int statusCode) {
switch (statusCode / 100) {
case 1:
return Family.INFORMATIONAL;
case 2:
return Family.SUCCESSFUL;
case 3:
return Family.REDIRECTION;
case 4:
return Family.CLIENT_ERROR;
case 5:
return Family.SERVER_ERROR;
default:
return Family.OTHER;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的代码在checkstyle和PMD规则中失败。说100和5是一个幻数。
我不想比较每个状态的服务器端异常代码的返回状态代码,是否还有其他更好的方法可以用来检查返回代码是否属于服务器错误家族。
httpresponse ×10
javascript ×4
java ×3
ajax ×1
android ×1
browser ×1
cocoa ×1
cookies ×1
cors ×1
credentials ×1
cross-domain ×1
exception ×1
forms ×1
html ×1
http-post ×1
httprequest ×1
jersey ×1
json ×1
post ×1
python ×1
string ×1