简体   繁体   English

如何在 scope 中从 ajax 成功分配变量

[英]How to assign the variable from ajax success within scope

The aim of the code is to set a box over an image using the values from ajax call该代码的目的是使用 ajax 调用中的值在图像上设置一个框

The code I tried is below,我试过的代码如下,

Code:代码:

   let neededvalue;

   $.ajax({
   type:'GET',
   url: 'getfile.php',

   data:{
     'file':file,

   },
   success: function(data){
     neededvalue = data.split(",");    // 2423100106

     console.log(neededvalue[0])      //23
     console.log(neededvalue[1])      //24
     console.log(neededvalue[2])      //100
     console.log(neededvalue[3])      //106

     $('#img01').selectAreas({
       onChanged: debugQtyAreas,
       maxAreas:1,
       areas: [
                  {
                    x: neededvalue[0],
                    y: neededvalue[1],
                    width: neededvalue[2],
                    height: neededvalue[3],
                  }
                ],

         parent: $('#myModal'),

     });
  }
})

In the above code, when I checked the values of neededvalue that i received from ajax Get, it prints the correct values in the log.在上面的代码中,当我检查从 ajax Get 收到的所需值的值时,它会在日志中打印正确的值。 I added those values to the side of the variables in the code above.我将这些值添加到上面代码中变量的一侧。 The problem that I face is when I try to use the values of neededvalue[0],neededvalue[1],neededvalue[2],neededvalue[3] and use them inside the function selectareas.我面临的问题是,当我尝试使用需要值 [0]、需要值 [1]、需要值 [2]、需要值 [3] 的值并在 function 选择区域内使用它们时。 I could not place the values from 'neededvalue' to variables x, y, width and height.我无法将“neededvalue”中的值放入变量 x、y、宽度和高度。

I tried to run the code with direct values to variables inside selectareas ie, x=23, y=24, width =100 and height=106 and I was able to make the box over the image.我尝试使用选择区域内变量的直接值运行代码,即 x=23、y=24、宽度 =100 和高度 =106,并且我能够在图像上制作框。

But I dont know why I can not use the values of "neededvalue" and assign it to the variables of selectareas x,y,width and height.但我不知道为什么我不能使用“neededvalue”的值并将其分配给选择区域x、y、宽度和高度的变量。

Can someone help me to fix this issue and help me assign the values from ajax success function to SelectAreas function有人可以帮我解决这个问题并帮我将 ajax 成功 function 的值分配给 SelectAreas function

What do the contents of the data variable look like? data变量的内容是什么样的? If it does not contain any commas, calling split(",") on it will return an array with only one index.如果它不包含任何逗号,则对其调用split(",")将返回一个只有一个索引的数组。

If your comment: // 2423100106 is a representation of the data variable, then the reason I stated above is why you can't assign those values to x , y , width and height .如果您的评论: // 2423100106data变量的表示,那么我上面所说的原因就是为什么您不能将这些值分配给xywidthheight If you want to have it work like that, the response of getfile.php should be: 24,23,100,106 .如果你想让它像那样工作, getfile.php的响应应该是: 24,23,100,106

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM