简体   繁体   中英

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

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. 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. I could not place the values from 'neededvalue' to variables x, y, width and height.

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.

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.

Can someone help me to fix this issue and help me assign the values from ajax success function to SelectAreas function

What do the contents of the data variable look like? If it does not contain any commas, calling split(",") on it will return an array with only one index.

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 . If you want to have it work like that, the response of getfile.php should be: 24,23,100,106 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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