Javascript 使用jquery将ajax调用返回值分配给var

Javascript 使用jquery将ajax调用返回值分配给var,javascript,ajax,jquery,global-variables,Javascript,Ajax,Jquery,Global Variables,如何将变量(var)分配给从ajax调用返回到服务器的json对象?我需要进入身体其他部位的物体 例如,我试过这个,我不知道它是正确的 var selectValues=$(document).ready(function() { $.ajax({ type: "POST", url: "http://10.0.2.2/mobileajax/callajax.php", data:

如何将变量(var)分配给从ajax调用返回到服务器的json对象?我需要进入身体其他部位的物体

例如,我试过这个,我不知道它是正确的

var selectValues=$(document).ready(function() {
    $.ajax({
                  type: "POST",
                  url: "http://10.0.2.2/mobileajax/callajax.php",
                  data: ({name: theName}),
                  cache: false,
                  dataType: "text",
                  success: onSuccess
                });
})


var $vendor = $('select.mobile-vendor');
var $model = $('select.model');
$vendor.change(
              function() {
                  $model.empty().append(function() {
                      var output = '';
                      $.each(selectValues[$vendor.val()], function(key, value) {
                          output += '<option>' + key + '</option>';
                      });
                      return output;
                  });
              }).change();

// bonus: how to access the download link
$model.change(function() {
    $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
});
var-selectValues=$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“http://10.0.2.2/mobileajax/callajax.php",
数据:({name:theName}),
cache:false,
数据类型:“文本”,
成功:成功
});
})
var$vendor=$('select.mobile vendor');
var$model=$('select.model');
$vendor.change(
函数(){
$model.empty().append(函数()){
var输出=“”;
$.each(选择值[$vendor.val()],函数(键,值){
输出+=''+键+'';
});
返回输出;
});
}).change();
//奖励:如何访问下载链接
$model.change(函数(){
$('a#下载链接').attr('href',selectValues[$vendor.val()][$model.val()]).show();
});

请注意,变量
selectValues
用于正文的其余部分。

必须在
文档就绪
范围外定义变量,如下所示:

var selectValues;
$(document).ready(function() {
    // ajax
});
$(document).ready(function() {
        var selectValues;
        $.ajax({
              type: "POST",
              url: "http://10.0.2.2/mobileajax/callajax.php",
              data: ({name: theName}),
              cache: false,
              dataType: "text",
              async: false,
              success: function(data) {
                selectValues = data
              }
            });

            var $vendor = $('select.mobile-vendor');
            var $model = $('select.model');
            $vendor.change(
                          function() {
                              $model.empty().append(function() {
                                  var output = '';
                                  $.each(selectValues[$vendor.val()], function(key, value) {
                                      output += '<option>' + key + '</option>';
                                  });
                                  return output;
                              });
                          }).change();

            // bonus: how to access the download link
            $model.change(function() {
                $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
            });
})

在onSuccess函数中,您可以定义
selectValues=data
或类似的内容

我认为您应该在
$(文档)中完成整个代码
函数,然后使ajax调用同步。因为现在ajax调用将在文档准备就绪时进行,但其他代码将直接运行,而无需等待文档准备就绪。此外,默认情况下ajax调用是异步的,因此您应该使其同步,并在其中指定
selectValues
变量ajax调用的success函数。它将变成如下内容:

var selectValues;
$(document).ready(function() {
    // ajax
});
$(document).ready(function() {
        var selectValues;
        $.ajax({
              type: "POST",
              url: "http://10.0.2.2/mobileajax/callajax.php",
              data: ({name: theName}),
              cache: false,
              dataType: "text",
              async: false,
              success: function(data) {
                selectValues = data
              }
            });

            var $vendor = $('select.mobile-vendor');
            var $model = $('select.model');
            $vendor.change(
                          function() {
                              $model.empty().append(function() {
                                  var output = '';
                                  $.each(selectValues[$vendor.val()], function(key, value) {
                                      output += '<option>' + key + '</option>';
                                  });
                                  return output;
                              });
                          }).change();

            // bonus: how to access the download link
            $model.change(function() {
                $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
            });
})
$(文档).ready(函数(){
var值;
$.ajax({
类型:“POST”,
url:“http://10.0.2.2/mobileajax/callajax.php",
数据:({name:theName}),
cache:false,
数据类型:“文本”,
async:false,
成功:功能(数据){
选择值=数据
}
});
var$vendor=$('select.mobile vendor');
var$model=$('select.model');
$vendor.change(
函数(){
$model.empty().append(函数()){
var输出=“”;
$.each(选择值[$vendor.val()],函数(键,值){
输出+=''+键+'';
});
返回输出;
});
}).change();
//奖励:如何访问下载链接
$model.change(函数(){
$('a#下载链接').attr('href',selectValues[$vendor.val()][$model.val()]).show();
});
})

试试这个。

下面是我们如何从(xml)ajax调用中提取返回的信息:

$.ajax ({
    type: "POST",
    url: "something.cgi?someparam=whatever",
    data: "key=val&key2=val2",
    dataType: "xml", // you use json, but I don't think it matters
    success: function (data) {
    if ($("error", data).text() === "") {
        // I could assign $("error", data).text() to a var just here
        // This gets the "error" parameter out of the returned xml or
        // json, here contained in "data"
    }
    [snip the rest]

另一种方法是在成功回调中添加其余代码,如下所示:

$(document).ready(function() {
        $.ajax({
              type: "POST",
              url: "http://10.0.2.2/mobileajax/callajax.php",
              data: ({name: theName}),
              cache: false,
              dataType: "text",
              async: false,
              success: function(selectValues) {
                var $vendor = $('select.mobile-vendor');
                var $model = $('select.model');
                $vendor.change(
                              function() {
                                  $model.empty().append(function() {
                                      var output = '';
                                      $.each(selectValues[$vendor.val()], function(key, value) {
                                          output += '<option>' + key + '</option>';
                                      });
                                      return output;
                                  });
                              }).change();

                // bonus: how to access the download link
                $model.change(function() {
                    $('a#download-link').attr('href', selectValues[$vendor.val()][$model.val()]).show();
                });
              }
            });

})
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“http://10.0.2.2/mobileajax/callajax.php",
数据:({name:theName}),
cache:false,
数据类型:“文本”,
async:false,
成功:函数(选择值){
var$vendor=$('select.mobile vendor');
var$model=$('select.model');
$vendor.change(
函数(){
$model.empty().append(函数()){
var输出=“”;
$.each(选择值[$vendor.val()],函数(键,值){
输出+=''+键+'';
});
返回输出;
});
}).change();
//奖励:如何访问下载链接
$model.change(函数(){
$('a#下载链接').attr('href',selectValues[$vendor.val()][$model.val()]).show();
});
}
});
})

将数据分配给
selectValues
?我需要分配返回值,而不是发送值。对不起,我指的是
响应
,类似于
success:function(response){selectValues=response;}
非常感谢您的快速响应!只要文档中的其余代码未准备就绪,这就无法工作..因为其余代码将在分配所选值之前执行。为true,但它仅由
更改
事件触发:)
var-selectValues
仅在
success
功能中可用,不在该范围之外。然后将“var-selectValues”定义为globalindite,如果
selectValues
是全局的,则这应该是您对Kugathasan AbimaranEven的回答,请注意,在成功回调运行之前,页面的其余部分实际上无法访问这些值。