Confusion in Angular 2 Http Post Request

后端 未结 2 686
一个人的身影
一个人的身影 2021-01-26 12:16

First of all. Please bear with my questions.

What I am doing is just performing an ajax request which will return a response data of string.

Here\'s my php

相关标签:
2条回答
  • 2021-01-26 12:28

    Actually Angular looks for a backend server to post the data, so Imagine in your post request is like below

    this.http.post('./app/parsers/peenoise.php', '', options).then(val => console.log(val))
    

    It will try to make the post request at the location imagine you are serving angular on localhost:4200

    http://localhost:4200/app/parsers/peenoise.php
    

    Now to answer your Questions :

    1)./app/parsers/peenoise.php doesn't work because it's actually trying to find it from your backend service

    2)You need to put header('Access-Control-Allow-Origin: *') whenever you are making an http request to a different server than from the one which your site is currently using, if you don't use that you'll end up with Connection_refused_Error.

    3)If you want to use just angular then there are mockAckends, in-memory-web-api

    An example using mockBackEnd is here

    0 讨论(0)
  • 2021-01-26 12:41

    Post requires a full or a relative URL to work. You can use:

    this.http.post('/parsers/peenoise.php', '', options)
    
    0 讨论(0)
提交回复
热议问题