简体   繁体   中英

how can i send http post request from localhost:5000 to localhost:3000

I'm trying to send a http post request from application running on localhost:5000 to my server at localhost:3000

this angular script is running on localhost:5000

    var data = {name:"john", description:"100"};
    var url = 'localhost:3000/razor';

    $http.post(url, data).then(function(response){
      if(response.data){
        console.log("success");
      } else {
        console.log("failure");
      }
    });

localhost:3000 is not receiving any POST request. What should I do? Error in console -

Error: A network error occurred.
rg/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:107:276
p@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:102:434
n/b<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:100:207
h/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:133:460
$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:147:309
$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:144:412
$evalAsync/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:147:398
f@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:47:146
kg/k.defer/c<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js:50:68
 Possibly unhandled rejection: {}

Without http in front of the url, the url becomes relative to the current url which is ' http://localhost:5000 ', so the url that you're posting to becomes ' http://localhost:5000/localhost:3000/razor ', but when you add http in front of the url, it becomes absolute url.

You need to add http in front of url path to make it absolute URL Try replacing this once

var url = ' http://localhost:3000/razor ';

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM