在我使用phonegap制作的Android应用程序中,用户可以登录.当他们这样做时,JQuery获取他们的登录名和密码,并根据HTTP Basic Auth规范使用他们的凭据进行ajax调用.一切正常.
但是,当用户设置错误的凭据时,我遇到了处理这种情况的问题.在.error()回调中,我想显示状态401的特定消息(如"身份验证失败")和其他状态的消息("技术问题").我的代码:
$.ajax({
url: "http://exemple.com",
type: "GET",
dataType: "text",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
beforeSend: function (xhr)
{
xhr.setRequestHeader("Authorization", "Basic "+$.base64.encode(email+":"+password));
},
success: function() {
//my success callback
},
error: function(xhr) {
if(xhr.status===401) {
//"authentication failed"
}
else {
//"technical problem"
}
}
});
Run Code Online (Sandbox Code Playgroud)
此代码在浏览器中正常工作.状态401被发送.但是,当我在我的phonegap android应用程序中测试时,状态发送为0.当我的凭据良好时,状态发送为200,而我的应用程序的其他请求运行良好,所以我不认为这是一个cors问题.
任何的想法 ?谢谢你的帮助
我正在为我的iOS应用程序使用Web服务.我知道如何通过URL(而不是http Body)发送http post请求并使用NSURLConnection Delegate获取响应.但是现在我有更好的方法来跟踪Web服务并在请求体中传递参数.我在谷歌上找了库,发现了这个.
但代码对我来说有点困难,我想应该有相同的功能,这可以使这一点更容易.有没有?到目前为止的代码如下.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:**WebService URL**]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/json" forHTTPHeaderField:@"Content-type"];
NSString *jsonString = [NSString stringWithFormat:@"{\n\"username\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":%@,\n\"%@\":%@,\n\"version\":%@,\n\"name\":\"%@\"\n}",userName, @"password",pass,@"accessToken",token,@"isOnline",@"True",@"accountType",type,@"False",name];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
这段代码中的jsonString是http体.我想在请求体中传递参数.现在,执行此代码后,我在NSData变量中得到null.而我的Web服务函数返回一个值,如果执行成功,它也会返回.我的代码有什么问题.谢谢.
我有一个完成的AngularJS应用程序,它基本上显示了一个表,其中包含许多(400+)行$resources.数据是可排序和可搜索的,应用程序加载有点慢(2s +),是的,我用过$cacheFactory.我很瘦,这是一个RAM问题.
我想到只加载100个条目并在搜索,排序或滚动后自动重新加载.这会加速应用程序,但搜索和排序真的很慢.
你会如何在Angular中处理这个问题?
我正在使用WordPress并使用jQuery fancyBox显示YouTube视频,但我收到此错误:
XMLHttpRequest cannot load http://www.youtube.com/embed/L9szn1QQfas?autoplay=1. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我jQuery code在footer.php中添加了bellow
$(document).ready(function() {
$(".fancybox").fancybox();
});
Run Code Online (Sandbox Code Playgroud)
并在我的帖子中添加了低于html的代码.
<a class="fancybox fancybox.iframe" href="http://www.youtube.com/embed/L9szn1QQfas?autoplay=1">
<img class="alignleft size-thumbnail wp-image-583" alt="small-screen-youtube" src="http://localhost/example/wp-content/uploads/2013/10/small-screen-youtube-150x150.png" width="150" height="150" />
</a>
Run Code Online (Sandbox Code Playgroud)
这个问题杀了我:(
任何想法或建议?谢谢.
我正在尝试使用node-xmlhttprequest.所以,如果我喜欢这样的话
// f.js
(function()
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
xhr = new XMLHttpRequest()
xhr.open('GET', "http://url-bla-bla.json", true)
xhr.onreadystatechange = function(){
console.log("xhr "+JSON.stringify(xhr))
}
xhr.send()
)()
Run Code Online (Sandbox Code Playgroud)
并称之为node f(假设链接Access-Control-Allow-Origin不是问题http://url-bla-bla.json- 它有效
但如果我尝试将相同的东西包装到Mocha spec中
describe('foo',function(){
it('xhr test',function(){
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
xhr = new XMLHttpRequest()
xhr.open('GET', "http://url-bla-bla.json", true)
xhr.onreadystatechange = function(){
console.log("xhr "+JSON.stringify(xhr))
}
xhr.send()
}
}
Run Code Online (Sandbox Code Playgroud)
并运行mocha f(我甚至不必将其包装在Mocha规范中)
它不会工作,也不会失败 readyState == 1
我刚开始学习如何使用API,但我在理解如何使用它们时遇到了一些麻烦.
由于一些在线文档,我能够编写以下代码,但是我对如何添加它有一些疑问:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml", false); xhr.send();
console.log(xhr);
Run Code Online (Sandbox Code Playgroud)
现在,当我在浏览器中运行此代码并打开控制台时,我会得到一个包含大量内容的下拉列表.首先,我如何让代码显示响应?我希望控制台只显示我运行代码时要显示的XML.其次,我如何解析XML?有没有办法从XML元素中获取值并将其分配给JavaScript变量?
谢谢!
我<input type="file" multiple />用来上传文件列表。这样可以正常工作,但是我希望能够在上传之前删除单个文件,因此我将FileList单独存储在一个对象中并通过xhr进行路由。但是,它不起作用。
该表格如下所示:
<form:form commandName="documentsBean" enctype="multipart/form-data">
<input type="hidden" name="submittedFormAction" value="attachDocumentSave"/>
<input type="file" name="files" id="attachFiles" multiple/>
<button type="submit" id="attachButton" onclick="return buildForm(this.form);">Attach</button>
</form:form>
Run Code Online (Sandbox Code Playgroud)
这是处理它的功能(工作版本):
function buildForm(form){
var formData = new FormData(form);
formData.append('testString', "foobar");
var xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
xhr.send(formData);
return false;
}
Run Code Online (Sandbox Code Playgroud)
非工作版本,我尝试将文件手动粘贴到formData中:
function buildForm(form){
var files = document.getElementById('attachFiles').files;
// var tempfiles = [];
// for(var i=0; i<files.length; i++){
// tempfiles[i]=files[i];
// }
var formData = new FormData();
formData.append('submittedFormAction', "attachDocumentSave");
formData.append('files', files); // …Run Code Online (Sandbox Code Playgroud) 我想在Instagram上调用此类链接,我正在使用以下代码,但它无法正常工作.出现以下错误,请帮助我如何从本地主机和服务器调用此URL.
var likeurl = "https://api.instagram.com/v1/media/" + idslist[1] + "/likes?ACCESS_TOKEN=" + accesstoken;
Nextin_downloadData(likeurl);
$.ajax({
url: likeurl,
type: 'POST',
data: {
},
success: function (result) {
alert("out" + result);
},
error: function () {
alert("error");
}
});
Error : XMLHttpRequest cannot load https://api.instagram.com/v1/media/714346267865083830_540073674/likes? ACCESS_TOKEN=1281148571.506af84.fc2e95e7257fdhgu5thca. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2356' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud) 我正在研究基于xml内容显示图像的内容流.我正在建立它作为一个网站,所以它是HTML和JavaScript,并且由于一些未知的原因 - 并且相信我,我已经用尽可能好的齿梳梳理它 - 我的javascript只是没有加载我的xml (那就是我很确定问题是什么,但我可能错了)而且它给我带来了'getElementsByTagName'为空的错误.现在我还没有内置完整的功能,我只是想通过把它作为文本读出来测试xml,所以请耐心等待.
loadXMLDoc.js:
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // code for IE5 and IE6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
Run Code Online (Sandbox Code Playgroud)
我的内部java在html中:
<script>
xmlDoc = loadXMLDoc("test.xml");
var x = xmlDoc.getElementsByTagName("song");
for (i=0; i<x.length; i++);
{
var a = (x[i].getElementsByTagName("source")[0].childnodes[0].nodeValue);
var b = (x[i].getElementsByTagName("artist")[0].childnodes[0].nodeValue);
var c = (x[i].getelementsByTagName("title")[0].childnodes[0].nodeValue);
var par = '<img class= "item" href ="#" ondblclick = "confirm()" src ="' +'" "title="' +b+ '"-"'+ c+ ' " />';
document.getElementById("demo").innerHTML …Run Code Online (Sandbox Code Playgroud) 我需要XMLHttpRequest在Delphi中使用二进制图像数据.我使用以下代码,但它不起作用,有人可以告诉我这个代码有什么问题,提前谢谢.
//I am using this function to get Image Binary data into Memory Stream.
procedure SendGETRequest(p_Url: string; p_resStream: TMemoryStream);
begin
FXmlHttpReq.open(METHOD_GET, p_Url, false, FUsername, FPassword);
FXmlHttpReq.setRequestHeader(HTTP_AUTHENTICATION, HTTP_BASIC + EncodeBase64(
FUsername + ':'+FPassword));
FXmlHttpReq.setRequestHeader(HTTP_CACHE_CONTROL, HTTP_NO_CACHE);
//FXmlHttpReq.setRequestHeader('Content-type','application/octet-stream');
FXmlHttpReq.send('');
if not VarIsEmpty(FXmlHttpReq.responseBody) then
begin
p_resStream:= OleVariantToMemoryStream(FXmlHttpReq.responseStream);
end;//if...
end;
function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size); …Run Code Online (Sandbox Code Playgroud)