标签: xmlhttprequest

阻止xmlHttpReq的浏览器缓存

似乎除了Opera之外的所有浏览器都在我的Javascript程序中缓存XHR请求.XHR请求中的URL是指服务器上的CGI程序,每次调用它时都会生成不同的输出.B Javascript中是否有一种方法可以使浏览器不缓存XHR请求?

javascript ajax xmlhttprequest

11
推荐指数
1
解决办法
2万
查看次数

php setcookie没有使用ajax调用

我有一个页面test.php,代码如下:

<html>
    <body>
        <form>
            <script type="text/javascript"> 

                function SendCookies(){

                    if (window.XMLHttpRequest)/* code for IE7+, Firefox, Chrome, Opera, Safari */
                    { xmlhttp=new XMLHttpRequest(); }
                    else /* code for IE6, IE5 */
                    { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

                    xmlhttp.onreadystatechange=function()
                    {
                        if (xmlhttp.readyState==4 && xmlhttp.status == 200)
                        {
                            alert('done');
                        }
                    }

                    xmlhttp.open("GET", "/web/DEV/Classes/SetCookie.php?time=" + new Date());
                    xmlhttp.send();

                }

            </script>

            <input type="text" id="txtInput" name="txtInput"/>
            <input type="button" id="btnSubmit" name="btnSubmit" value="Submit"  onclick="SendCookies()"/>
            <div id="divTest">
                <?php
                    if (isset($_COOKIE["TestCookie"])) {
                        echo $_COOKIE["TestCookie"];
                    } else {
                        echo "__Results__";
                    }
                ?>          
            </div>
        </form> …
Run Code Online (Sandbox Code Playgroud)

javascript php cookies ajax xmlhttprequest

11
推荐指数
2
解决办法
2万
查看次数

将Ajax与Cookie一起使用

我正在尝试为我的网站创建一个与Imgur集成的拖放界面.问题是我正在尝试使用带有cookie的ajax.我没有登录就让它工作,但我需要将照片放在我的帐户下.理论上我的代码应该可以工作,但在实践中由于某种原因,ajax请求/ cookie不起作用.我究竟做错了什么?感谢:D

我的代码:http://jsfiddle.net/msm595/9arFd/ 我的用户名和密码不在那里(尽管我正在使用虚拟帐户进行测试).

javascript cookies ajax xmlhttprequest cross-domain

11
推荐指数
1
解决办法
5060
查看次数

使用javascript从服务器加载到图像的二进制图像

我正在通过XMLHttpRequest以二进制格式从我的服务器加载jpeg图像(我需要这样).它不是base64编码的.

是否可以使用javascript将其转换为img对象?

谢谢

html javascript binary dom xmlhttprequest

11
推荐指数
1
解决办法
5701
查看次数

为什么这个jQuery AJAX PUT在Chrome中工作但不在FF中工作

在Chrome中,它会像它应该的那样执行HTTP PUT,但在FireFox 21中却没有.javascript控制台或后端没有错误.

这是HTML:

<div id="createTeamModal" class="small reveal-modal">
        <form id="createTeamForm">
            <div class="row"><p id="teamFlavorText" class="lead">Building a new team</p></div>
            <div class="row">
                <div class="small-4 large-4 columns"><label>Team Name:</label></div>
                <div class="small-6 large-6 columns"><input name="teamName" id="teamName" type="text" size="20"/></div>
            </div>
            <div class="row"><p class="lead">Does this team work for a business?</p></div>
            <div class="row">
                <div class="small-4 large-4 columns"><label>Business Size:</label></div>
                <div class="small-6 large-6 columns">
                    <select id="businessSizeSelect" name="businessSizeSelect">
                    <option value="1">Nope, I work alone</option><option value="2">2 to 49</option><option value="3">50 to 99</option><option value="4">100 to 999</option><option value="5">1,000+</option>
                    </select>
                </div>
            </div>
            <div id="businessLocationDiv" class="row" style="display: none; margin-top: …
Run Code Online (Sandbox Code Playgroud)

ajax firefox jquery xmlhttprequest http-put

11
推荐指数
1
解决办法
5018
查看次数

XMLHttpRequest同源策略

我花了最后3天研究如何使用XMLHttpRequest创建跨域请求.最好的替代方案确实是我已经使用的JSONP.

但我还有一个问题,我无法找到答案.我阅读了数百个帖子(包括SO),没有人有一个很好的可靠回答(很好的参考).希望有人可以提供帮助.

说,我在许多网站上读到,由于安全原因,我无法从域aaa.com向bbb.com发出Ajax请求并获取我想要的数据.这很清楚,我对此毫无疑问.但问题是当我在我的localhost中运行下面的代码时(所以我的域名是"localhost",我不应该从另一个域请求任何数据).

xhReq = new XMLHttpRequest();
xhReq.open("GET","http://domain.com?parameter",true);
xhReq.send(null);
Run Code Online (Sandbox Code Playgroud)

当我检查Firebug Net选项卡时,我发现请求未被阻止!明确要求.我简直不敢相信.所以我在domain.com/log.php中创建了一个文件,在那里我可以记录任何访问我域名的请求.令人惊讶的是,我解雇localhost的所有请求都是我的域名网站.当我尝试获取响应时,由于我的Chrome和FIrebug浏览器的原始策略相同,我实在无法得到它.但我真的很惊讶,尽管我无法操纵响应,但请求真的打到了网络服务器.

更令人惊讶的是,如果domain.com/log.php生成一个像1MB这样大的响应,我的firebug告诉我浏览器确实从网络服务器下载了所有1MB,并且最后它按预期显示"拒绝访问"消息.因此,如果相同的原始策略禁止读取该数据,为什么要下载所有文件.

最后,我让我惊讶的是,我读到的所有网站和规范都说非常清楚,当目标域与源域不匹配时,使用Ajax阻止了请求.但显然,通过我的实验,尽管我无法访问响应数据,但请求仍在完成.

令我不安的是,它可能会打开一个大的安全漏洞,每天有数千个视图的网站可以运行这个3行代码,并在一个不友好的网站上引起巨大的Ddos攻击,只是让用户在另一个网站请求一个页面由于浏览器不会阻止请求,因此间隔较小.

我在IE 7,8和9以及Chrome最新和Firefox最新版本中测试了这个脚本,行为是相同的:请求已完成,浏览器下载了所有响应,但没有让它可以执行SOP.

希望有人可以解释为什么规格是如此错误或者我理解错误!

javascript ajax xmlhttprequest

11
推荐指数
1
解决办法
5384
查看次数

Chrome上的XMLHttpRequest onprogress total总是为0

我试图在同一网站内从一个页面导航到另一个页面之前显示进度条.

我的函数将updateProgress函数绑定到XMLHttpRequest onprogress事件,并将用户重定向到新页面(xhr.readyState == 4 && xhr.status == 200)似乎工作正常,但Chrome显示"总计"为零,赢得了' t让进度条正常运行.我的代码如下.

先感谢您...

$('.ajaxNavi').click(function (e) {
  e.preventDefault();
  var url = $(this).attr('href');
  var xhr = new XMLHttpRequest();
  xhr.onprogress = updateProgress;
  xhr.onreadystatechange = function () { 
    if (xhr.readyState == 4 && xhr.status == 200)
    // REDIRECT HERE
    }
  });
  xhr.open("GET", url, true);
  xhr.setRequestHeader("Content-Type", "text/html");
  xhr.send();
});

function updateProgress(e) {
  console.log(e.loaded + '    ' + e.total);
}
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery google-chrome xmlhttprequest

11
推荐指数
1
解决办法
3407
查看次数

如何将数据体发送到XMLHttpRequest,如下所示?

如何正确格式化?

var params = {
  "range":"Sheet1!A4:C4",
  "majorDimension": "ROWS",
  "values": [
    ["Hello World","123", "456"]
  ],
}
Run Code Online (Sandbox Code Playgroud)

然后使用POST发送它,如:

   var xhr = new XMLHttpRequest();
   xhr.open(method, url);
   xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
   xhr.onload = requestComplete;
   xhr.send(params);
Run Code Online (Sandbox Code Playgroud)

我知道我会遇到错误,因为有一种格式化我的"请求体"的正确方法.它看起来像是数组和JSON的混合,所以我请求你的帮助如何正确格式化它.

javascript xml ajax http xmlhttprequest

11
推荐指数
2
解决办法
2万
查看次数

将FormData转换为查询字符串的更简单方法

我正在通过XMLHttpRequest输入HTML表单的数据发送POST请求.不受JavaScript干扰的表单将提交其编码为的数据application/x-www-form-urlencoded.

使用XMLHttpRequest,我想通过FormDataAPI 发送数据,这不起作用,因为它将数据视为编码为multipart/form-data.因此,我需要将数据作为查询字符串,正确转义,写入到send方法中XMLHttpRequest.

addEntryForm.addEventListener('submit', function(event) {
    // Gather form data
    var formData = new FormData(this);
    // Array to store the stringified and encoded key-value-pairs.
    var parameters = []
    for (var pair of formData.entries()) {
        parameters.push(
            encodeURIComponent(pair[0]) + '=' +
            encodeURIComponent(pair[1])
        );
    }

    var httpRequest = new XMLHttpRequest();
    httpRequest.open(form.method, form.action);

    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState === XMLHttpRequest.DONE) {
            if (httpRequest.status === 200) {
                console.log('Successfully submitted the request');
            } …
Run Code Online (Sandbox Code Playgroud)

javascript xmlhttprequest form-data

11
推荐指数
3
解决办法
1万
查看次数

是否可以使用WebAssembly提交HTTP请求?

我正在尝试在WebAssembly中提交一个简单的HTTP GET请求.为此,我编写了这个程序(从Emscripten网站复制,稍作修改):

#include <stdio.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/fetch.h>
#include <emscripten.h>
#endif


void downloadSucceeded(emscripten_fetch_t *fetch) {
  printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
  // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
  emscripten_fetch_close(fetch); // Free data associated with the fetch.
}

void downloadFailed(emscripten_fetch_t *fetch) {
  printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
  emscripten_fetch_close(fetch); // Also free data on failure.
}

unsigned int EMSCRIPTEN_KEEPALIVE GetRequest() {
  emscripten_fetch_attr_t attr;
  emscripten_fetch_attr_init(&attr);
  strcpy(attr.requestMethod, "GET");
  attr.attributes …
Run Code Online (Sandbox Code Playgroud)

c http xmlhttprequest emscripten webassembly

11
推荐指数
2
解决办法
3272
查看次数