1

I would like to know how to make ajax call in div without button click in javascript

Also, I will pass corresponding id to the ajax function in javascript

function handleCall(pr_id) {
  var li = $.ajax({
    url: "/en/send-money-from-" + sourcecn + "-to-" + targetcn,
    method: 'get',
    global: false,
    async: false,
    data: {
      idvalue: pr_id
    },
    success: function(value) {
      return value;
    }
  }).responseText;
  console.log(JSON.parse(li));
}

render() {
  <div id="trans" onload=${()=>this.handleCall(this.id)}> // not working
    <p>Cal the ajax function without click button and pass id to ajax function</p>
  </div>
}
Senthil
  • 961
  • 1
  • 8
  • 21
  • Are you using `react`? – Anurag Srivastava May 24 '19 at 15:36
  • Just put the `$.ajax()` call directly in a document.ready event handler and it will be called on load. You will need to change two things in your code, though. Firstly how you read pr_id` an secondly remove `async: false` as it's incredibly bad practice. Research how to work with callbacks properly: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Rory McCrossan May 24 '19 at 15:36
  • @AnuragSrivastava am using lit-element – Senthil May 24 '19 at 15:38
  • use setinterval, unless you had some other event in mind. – shadow2020 May 24 '19 at 15:39
  • @RoryMcCrossan thanks for reply, wil try, if you already familiar please share sample, thanks much – Senthil May 24 '19 at 15:43
  • I would if I could. All you need to do is copy+paste what you have in to a document.ready handler - which you should already have if you're using jQuery. I can't help you retrieve `pr_id` as you've not specified what value you're expecting it to have when you run this function without the context of an element – Rory McCrossan May 24 '19 at 15:44
  • @RoryMcCrossan thanks, `pr_id` is div `id` which is passed the to ajax call, – Senthil May 24 '19 at 15:49
  • Yes, but if you're running this immediately on load there is no way to know which `div` should be used. Unless you mean there is only ever one div..? – Rory McCrossan May 24 '19 at 15:52
  • What exactly does your `this` refer to here? It's either the `div` or the `Window` – A Rogue Otaku May 24 '19 at 15:54
  • @AmitDas refers to `div` – Senthil May 24 '19 at 15:55
  • @Senthil. Then how come you are accessing `this.handleCall`? As far as I can see, you `handleCall` is not a property of the `div` – A Rogue Otaku May 24 '19 at 15:57
  • @RoryMcCrossan my scenario is I have static data in div, once `div` loaded i need to make ajax call, but donknow how to make ajax call without button click, which event to use – Senthil May 24 '19 at 15:57
  • @AmitDas I got stuck how to access the function `handlecall` without button click and also pass `id` – Senthil May 24 '19 at 16:00
  • @Senthil, not sure but try this: `onload=${e=>handleCall(e.target.id)}` Since I'm not familiar with the framework you are using, it's really hard to understand the context of `this` here. – A Rogue Otaku May 24 '19 at 16:01

0 Answers0