0

I have to read information as below from a microprocessor hardware

var save_it = '';
$.ajax({
    type: "GET",
    url: 'http://www.micro-processor-server/?ROBOT=arm_controle',
    async: false,
    success:function(m) {
      save_it =  m;
    }
  }).responseText;
console.log(save_it);

While doing so, it works, but my browser console gives very scary warning as below, also several times I have noticed web-browser Google chrome get hang:

enter image description here

Can anyone please show an alternative way how to make my Ajax query compatible? (I can't change codes in Micro-processor its third party robot)

weston
  • 54,145
  • 21
  • 145
  • 203
  • 2
    async:true and learn how to use asynchronous code, rather than fear it – Jaromanda X Aug 26 '16 at 03:43
  • 1
    It probably hangs because of network lag because your request is synchronous. It waits for the response and if it takes a long time the browser will pause. Why can't it be asynchronous? – Jared Aug 26 '16 at 03:43
  • 2
    Just move your logging into the success function and get rid of the async flag... You can also make `save_it` into a local variable within the success callback. – Simon MᶜKenzie Aug 26 '16 at 03:44
  • async:true never worked. –  Aug 26 '16 at 03:45
  • if i remove the async:true or false it never gets any response at all, but Robot executes the logs in its own terminal, where $.ajax only works when async:false flag is used. –  Aug 26 '16 at 03:46
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 4castle Aug 26 '16 at 03:49
  • 1
    @YumYumYum: async:true works. You just need to learn how to write async code. – slebetman Aug 26 '16 at 04:43

2 Answers2

1

Put the code that needs the response in the success function. You could also use $.get if this is all you're needing to do.

$.get('http://www.micro-processor-server/?ROBOT=arm_controle', function(data) {
    console.log(data);
});
4castle
  • 32,613
  • 11
  • 69
  • 106
0

call another function with return data.

 $.get('http://www.micro-processor-server/?   ROBOT=arm_controle',function(data){
     myfun(data);
 });

 myfun(data){ 
    console.log(data);//here you might be able get rid of asynchronous behaviour.
 }
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • 2
    You can replace `function(data) {myfun(data);}` with `myfun`, e.g. `$.get('http://www.micro-processor-server/?ROBOT=arm_controle', myfun);`. – Simon MᶜKenzie Aug 26 '16 at 09:39