0

I have some old code which does ajax calls. I wrote a new angular 10 application, which needs to run the original ajax code directly into the angular service (per requirement). Is there a way to run an ajax call directly or a node module that can be used. Thanks for the help.

bflow1
  • 49
  • 1
  • 9
  • What does it use to do ajax calls? JQuery? XMLHttpRequest? – ShamPooSham Nov 18 '21 at 12:24
  • Found the answer here https://stackoverflow.com/questions/32050645/how-to-use-jquery-with-typescript – bflow1 Nov 19 '21 at 09:36
  • Ok cool, so you're using jquery. If this works for now, great. But I would advice you to tell your project owner that you think it's a bad requirement, and that it should be rewritten using angular http – ShamPooSham Nov 19 '21 at 09:54

1 Answers1

0
@Injectable({providedIn: 'root'})    
export class SomeService {
destroy$ = new Subject();
constructor(private http: HttpClient) { }
ajax(): Observable<DataType> {
        return this.http
          .get<DataType>(URL, {params: {email: 'tt@tt.tt'}})
          .pipe(
            takeUntil(this.destroy$), // will unsubscribe automatically if call this.destroy$.next(true) && this.destroy$.complete()
            map(data => /*map data here if need*/)
          );
      }
    }
  • u can remove pipe func – Никита Середа Nov 18 '21 at 12:19
  • Thanks! this is cool, but what I meant was is there a way to like copy/paste ajax code directly into angular. Similar to how you would call JavaScript code, doing a post method. – bflow1 Nov 18 '21 at 12:39
  • @bflow1 I asked a question in the comment section of your question, can you answer it? Saying you're using ajax says very little about what the code looks like. The answer is probably "yes", since Angular is a javascript framework. But you might need to tweak it to follow angular best practices and avoid pitfalls. I can't help you with tweaking it unless I know what your code does. – ShamPooSham Nov 18 '21 at 13:22
  • Basically, what we need is to call a JavaScript function, in angular that makes an ajax http post call. What they want is to copy the javascript code inside the angular service. They don't want to rewrite the javascript code, just call it. – bflow1 Nov 18 '21 at 13:48
  • @bflow1 Yes, I understand that. What I'm saying is that it should work, but you might want to wrap your code somehow and put it in the right place. To do that, I need to know what you mean by "ajax http post call", please answer my question. Do you have the code that makes the call? Can you share it? Also, have you simply tried pasting it into your project and see if it works? – ShamPooSham Nov 18 '21 at 14:18
  • @bflow1 Also, please tag me if I'm the one you're responding to, otherwise I don't get a notification. – ShamPooSham Nov 18 '21 at 14:18
  • if u will just put ajax code in Typed framework u can get a lot of errors after some time. try to rewrite ajax code always – Никита Середа Nov 19 '21 at 04:10