Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何强制jquery的ajax在执行以下语句之前等待_Jquery_Ajax - Fatal编程技术网

如何强制jquery的ajax在执行以下语句之前等待

如何强制jquery的ajax在执行以下语句之前等待,jquery,ajax,Jquery,Ajax,我有一个简单的代码示例来做两件事: 查询accept lang头(ajax) 使用/引用它(可以是ajax的一部分,因为这需要在很多地方,示例将显示在屏幕上。) html如下所示: <html> <head> <script src="jquery-1.11.0.js"></script> </head> <body> <H1 id=bLocale>Should be the browser locale h

我有一个简单的代码示例来做两件事:

  • 查询accept lang头(ajax)
  • 使用/引用它(可以是ajax的一部分,因为这需要在很多地方,示例将显示在屏幕上。)
html如下所示:

<html>
<head>
<script src="jquery-1.11.0.js"></script>
</head>
<body>
<H1 id=bLocale>Should be the browser locale here</H1>
</body>
<script>
var bLocale='unchanged';
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        bLocale= headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);
    },
    async:   false
});
$("#bLocale").text(bLocale);
</script>
</html>

应该是此处的浏览器区域设置
var bLocale='unchanged';
$.ajax({
url:“http://ajaxhttpheaders.appspot.com", 
数据类型:“jsonp”,
成功:函数(标题){
bLocale=headers['Accept-Language'];
逗号=bLocale.indexOf(',');
如果(逗号>0)bLocale=bLocale.substring(0,逗号);
},
异步:false
});
$(“#bLocale”).text(bLocale);
问题是ajax太慢(即使asyn设置为false),并且在bLocale获取返回值之前显示

有什么办法解决这个问题吗

问题是ajax太慢(即使asyn设置为false),并且在bLocale获取返回值之前显示

您告诉jQuery使用,这本质上是异步的<代码>异步:false对其没有影响
async:false
适用于真正的ajax请求,但不适用于JSONP。(不过,这不会持续很久;jQuery团队正在删除
async
选项,您必须直接使用
XMLHttpRequest
来获取同步请求。)

此外,您不需要同步请求。拥抱web开发的异步、事件驱动特性:

var bLocale='unchanged'; // Move this *into* the success function unless
                         // you really, really need it to be outside it
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        bLocale= headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);

        $("#bLocale").text(bLocale);  // <=== Inside the callback, not outside it
    }
});
var bLocale='unchanged';//将此*移到*成功函数中,除非
//你真的,真的需要它在外面
$.ajax({
url:“http://ajaxhttpheaders.appspot.com", 
数据类型:“jsonp”,
成功:函数(标题){
bLocale=headers['Accept-Language'];
逗号=bLocale.indexOf(',');
如果(逗号>0)bLocale=bLocale.substring(0,逗号);
$(“#bLocale”).text(bLocale);//
问题是ajax太慢(即使asyn设置为false),并且在bLocale获取返回值之前显示

您告诉jQuery使用,这本身就是异步的;
async:false
对它没有影响。
async:false
对真正的ajax请求有效,但对JSONP无效。(不过,这不会持续很久;jQuery团队正在删除
async
选项,您必须直接使用
XMLHttpRequest
来获取同步请求。)

此外,您不需要同步请求。请接受web开发的异步、事件驱动特性:

var bLocale='unchanged'; // Move this *into* the success function unless
                         // you really, really need it to be outside it
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        bLocale= headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);

        $("#bLocale").text(bLocale);  // <=== Inside the callback, not outside it
    }
});
var bLocale='unchanged';//除非
//你真的,真的需要它在外面
$.ajax({
url:“http://ajaxhttpheaders.appspot.com", 
数据类型:“jsonp”,
成功:函数(标题){
bLocale=headers['Accept-Language'];
逗号=bLocale.indexOf(',');
如果(逗号>0)bLocale=bLocale.substring(0,逗号);
$(“#bLocale”).text(bLocale);//
问题是ajax太慢(即使asyn设置为false),并且在bLocale获取返回值之前显示

您告诉jQuery使用,这本身就是异步的;
async:false
对它没有影响。
async:false
对真正的ajax请求有效,但对JSONP无效。(不过,这不会持续很久;jQuery团队正在删除
async
选项,您必须直接使用
XMLHttpRequest
来获取同步请求。)

此外,您不需要同步请求。请接受web开发的异步、事件驱动特性:

var bLocale='unchanged'; // Move this *into* the success function unless
                         // you really, really need it to be outside it
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        bLocale= headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);

        $("#bLocale").text(bLocale);  // <=== Inside the callback, not outside it
    }
});
var bLocale='unchanged';//除非
//你真的,真的需要它在外面
$.ajax({
url:“http://ajaxhttpheaders.appspot.com", 
数据类型:“jsonp”,
成功:函数(标题){
bLocale=headers['Accept-Language'];
逗号=bLocale.indexOf(',');
如果(逗号>0)bLocale=bLocale.substring(0,逗号);
$(“#bLocale”).text(bLocale);//
问题是ajax太慢(即使asyn设置为false),并且在bLocale获取返回值之前显示

您告诉jQuery使用,这本身就是异步的;
async:false
对它没有影响。
async:false
对真正的ajax请求有效,但对JSONP无效。(不过,这不会持续很久;jQuery团队正在删除
async
选项,您必须直接使用
XMLHttpRequest
来获取同步请求。)

此外,您不需要同步请求。请接受web开发的异步、事件驱动特性:

var bLocale='unchanged'; // Move this *into* the success function unless
                         // you really, really need it to be outside it
$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        bLocale= headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);

        $("#bLocale").text(bLocale);  // <=== Inside the callback, not outside it
    }
});
var bLocale='unchanged';//除非
//你真的,真的需要它在外面
$.ajax({
url:“http://ajaxhttpheaders.appspot.com", 
数据类型:“jsonp”,
成功:函数(标题){
bLocale=headers['Accept-Language'];
逗号=bLocale.indexOf(',');
如果(逗号>0)bLocale=bLocale.substring(0,逗号);
$(“#bLocale”).text(bLocale);//

没错,使用ajax实现同步是不可能的。由于返回的是jsonp,我们可以将其加载到脚本标记中。请参阅下面的内容。感谢所有提示我使用此解决方案的帮助。




var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);


var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);
应该是此处的浏览器区域设置

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);

var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);



var bLocale='raw'; // can be used at any other place

function processHeaders(headers){
    bLocale=headers['Accept-Language'];
    comma=bLocale.indexOf(',');
    if(comma>0) bLocale=bLocale.substring(0, comma);
}
$("#bLocale").text(bLocale);
没错,使用ajax实现同步是不可能的。由于返回的是jsonp,我们可以将其加载到脚本标记中。请参阅下面的内容。感谢所有提示我使用此解决方案的帮助。