Answers.Body: <p>You should try to keep your controllers as "thin" as possible. That is, they should call other classes, which do the actual work.</p>
  
  <p>The Craft approach is to keep most of your code in service classes, and if you're just starting out with plugin development, I recommend you follow this pattern.</p>
  
  <p>Bear in mind, though, that your service classes define your plugin's API.</p>
  
  <p>In other words, if you add a public method to one of your plugin service classes, you're essentially telling everyone "hey, you can use this method if you like".</p>
  
  <p>If you're interested, I wrote <a href="https://experiencehq.net/blog/the-hidden-perils-of-craft-plugin-service-classes" rel="nofollow noreferrer">a blog post</a> about this a while back.</p>
  
  <p>In summary, keeping your code in services is much better than keeping it in controllers, but don't treat your service classes as a dumping ground for everything.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T06:08:08.780Z
Answers.Text: If that's the case, what is the difference between service and controller?
Answers.UserDisplayName: lance adrianne dela cruz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T06:13:49.063Z
Answers.Text: Among many other things, controllers handle incoming HTTP requests. You should spend some time reading up on [Craft plugin development](https://craftcms.com/docs/plugins/introduction), the underlying [Yii framework](http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc), and [the MVC pattern](https://en.m.wikipedia.org/wiki/Model–view–controller) in general. StackExchange is not a substitute for independent study, and you need a solid understand of the basics.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T06:18:49.987Z
Answers.Text: No need to apologise, we're all learning. You're just going to struggle if you haven't taken the time to understand the basic concepts.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T08:26:27.090Z
Answers.Text: So the controller will be the one making the ajax call? correct me if I'm wrong sir :)
Answers.UserDisplayName: lance adrianne dela cruz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T14:21:42.100Z
Answers.Text: No, the service makes the call to the external API. The controller handles requests _to_ your plugin.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-27T04:59:33.720Z
Answers.Text: Ok sir. Thanks. Is it applicable if i perform a jquery ajax to make an http request?
Answers.UserDisplayName: lance adrianne dela cruz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-27T05:22:04.610Z
Answers.Text: HTTP requests to your plugin are handled by your controller, regardless of whether they use AJAX.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T01:21:05.523Z
Answers.Text: then what should i put int the services?
Answers.UserDisplayName: lance adrianne dela cruz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T01:23:46.213Z
Answers.Text: We've covered this, at length, in the original answer, and the subsequent comments.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T01:31:04.373Z
Answers.Text: My bad. I didn't see. I thought I make a new post.
Answers.UserDisplayName: lance adrianne dela cruz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T06:02:21.433Z
Answers.LastActivityDate: 2017-04-26T06:02:21.433Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>Where should I put my ajax call if I want to get some data from an external link or website? Should I put it in the controller, or in the service?</p>
  
Tags: <plugin-development>
Title: Where should I make an HTTP REQUEST using AJAX?
ThreadText: # Where should I make an HTTP REQUEST using AJAX?
  
  (Asked by: username_0 on 2017-04-26)
  
  Where should I put my ajax call if I want to get some data from an external link or website? Should I put it in the controller, or in the service?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-04-26. Score: 4 | Accepted answer
  
  You should try to keep your controllers as "thin" as possible. That is, they should call other classes, which do the actual work.
  
  The Craft approach is to keep most of your code in service classes, and if you're just starting out with plugin development, I recommend you follow this pattern.
  
  Bear in mind, though, that your service classes define your plugin's API.
  
  In other words, if you add a public method to one of your plugin service classes, you're essentially telling everyone "hey, you can use this method if you like".
  
  If you're interested, I wrote [a blog post](https://experiencehq.net/blog/the-hidden-perils-of-craft-plugin-service-classes) about this a while back.
  
  In summary, keeping your code in services is much better than keeping it in controllers, but don't treat your service classes as a dumping ground for everything.
  
  > **Comments:**
  > **username_0**: If that's the case, what is the difference between service and controller?  
  > **username_1**: Among many other things, controllers handle incoming HTTP requests. You should spend some time reading up on [Craft plugin development](https://craftcms.com/docs/plugins/introduction), the underlying [Yii framework](http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc), and [the MVC pattern](https://en.m.wikipedia.org/wiki/Model–view–controller) in general. StackExchange is not a substitute for independent study, and you need a solid understand of the basics.  
  > **username_1**: No need to apologise, we're all learning. You're just going to struggle if you haven't taken the time to understand the basic concepts.  
  > **username_0**: So the controller will be the one making the ajax call? correct me if I'm wrong sir :)  
  > **username_1**: No, the service makes the call to the external API. The controller handles requests _to_ your plugin.  
  > **username_0**: Ok sir. Thanks. Is it applicable if i perform a jquery ajax to make an http request?  
  > **username_1**: HTTP requests to your plugin are handled by your controller, regardless of whether they use AJAX.  
  > **username_0**: then what should i put int the services?  
  > **username_1**: We've covered this, at length, in the original answer, and the subsequent comments.  
  > **username_0**: My bad. I didn't see. I thought I make a new post.  
  
  ---
  
  