0

Update

I've put two project on one server which are client and server RestAPI applications. I set 4200 as a port for the Client (which is written by Angular 6) and port 8990 for Server side WebSrv. I run Spring one with the command java -jar artifact.jar, it works fine and response any requests. But for Client side when I run it from my local machine with IP : localhost:4200 ( either with IntellijIdea built-in builder or with Angular CLI) it works fine and can send the request and receive response. But when I run it from that server (same place that WebSrv server located) and run, it shows the first html page correctly but when I click the button for sending request, nothing happen (nor exception or any log) and server isn't receiving any request!!

I've searched for my issue but there was nothing helpful.I Would be grateful if anyone could help me to find the solution. I was wondering if anyone knows what's the problem.

Here is my Client side code (Angular)

import {Component, NgModule, OnInit} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {RouterModule, Router} from '@angular/router';

// const URL = 'http://localhost:8990/getUserId';
const URL = 'http://getuserid.mycompany.com:8990/getUserId';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {

  constructor(
    private http: HttpClient
  ) {
  }

  fileToUpload: File = null;
  id: String = '0';

  inputId(event) {
    this.id = event.target.value;
    console.log('id is -- > ' + event.target.value);
  }

  inputFile(event) {
    this.fileToUpload = event.target.files[0];
    console.log('File path -- > ' + event.target.files[0].name);
  }

  onSubmit(id: string, file: File) {
    const frmData = new FormData();
    console.log('POST');
    // @ts-ignore
    frmData.append('id', this.id);
    frmData.append('inputPackage', this.fileToUpload);
    console.log('id --> ' + this.id);
    console.log('File name --> ' + this.fileToUpload.name); 
    this.http.post(URL, frmData).subscribe(res => {
      const resp = JSON.parse(JSON.stringify(res));
      if (resp['user'] != null) {
          if (resp['user']['user-id'] === false) { 
            alert('Successful!! Your User ID is : ' + resp['user']['user-id']); 
          } else {
            alert('Sorry!! Error occurred : ' + resp['error-message']);
          }
     } else {
        alert('Sorry!! Error occurred : ' + resp['error-message'] );
      }
    });

  }
}

and this is my server side piece of code (Spring Boot):

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;



@Controller
public class GetUserIdController {
    private final static String DESTINATION_PATH = "/srv/resources/";
//    private final static String DESTINATION_PATH = "/mnt/d/DestPath/";
//    private final static String DESTINATION_PATH = "C:/Resources/Temp/";

    @CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
    @RequestMapping(method = RequestMethod.POST, value = "/getUserId",  headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
    @ResponseBody
    String Response(@RequestParam("inputPackage") MultipartFile[] inputPackages, @RequestParam("id") String id) {

        String response = null;
        String id ; 
        try {

            if (inputPackages != null && id != null && inputPackages.length > 0 && id.length() > 1) {

                ReceivedPackage recvPackage = new ReceivedPackage();
                recvPackage.setPId(id);
                if (inputPackages[0].getOriginalFilename() != null ) {
                    if( inputPackages[0].getOriginalFilename().contains(".zip")) {
                         FileUtils.saveFile(inputPackages[0].getInputStream(),inputPackages[0].getOriginalFilename(), DESTINATION_PATH);
                        recvPackage.setPackageName(inputPackages[0].getOriginalFilename());
                        recvPackage.setPackagePath(DESTINATION_PATH);  
                        recvPackage.setInputPackage(new File ( recvPackage.getPackagePath()));

                        response = GetUserId.runProcess(recvPackage, DESTINATION_PATH, id); 

                    }else{

                        response = "<error-message>The input file : "+ (inputPackages[0].getOriginalFilename())+" is invalid!!\n It should be a zip file!</error-message>";
                    }
                }
            }else{

                response = "<error-message>The ID and valid zip file should be provide!</error-message>" ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return FileUtils.getJSONFormat(response);
    }
}

Thanks in advance.

Far Zad
  • 389
  • 4
  • 11
  • Hi , Welcome to stack overflow .Please take the time to read to see https://stackoverflow.com/help/how-to-ask, – core114 Sep 27 '18 at 03:17
  • Thanks for your comment and help. I've read it before and tried to ask straight question which there were no similar in other places. So could you please let me know which part of my question and its description is bad or unclear that I will more careful in next time. Cheers, – Far Zad Sep 27 '18 at 03:46
  • You need to do a better job _describing_ the problem. And providing as much detail as possible. All you are saying right now is that you have two projects that don't work. What are we supposed to do with that information? – Paul Samsotha Sep 27 '18 at 03:46
  • Thanks @PaulSamsotha. My problem is there is no Error or Exception occurred. When I click button nothing happen. I though may be I have to do some different setting for this situation I mean when I want to run two different project on same server but on different port. What do you think I need to add in my description to make more clear? any part of code or something? Thanks in advance – Far Zad Sep 27 '18 at 04:08
  • 1
    Maybe describe a specific request in detail. What you expect to happen. What _is_ happening. What information are you observing in the browser developer tools during this request. If you don't provide enough information for us to reproduce the problem, then is hard for us to help you. This is laid out in the "how to ask" link provided by @core114. – Paul Samsotha Sep 27 '18 at 04:13
  • it may be issue of cros origin pls have look https://stackoverflow.com/questions/32319396/cors-with-spring-boot-and-angularjs-not-working – R. S. Sep 27 '18 at 05:27
  • Thanks @R.S. I've put mentioned class but no success! – Far Zad Sep 27 '18 at 21:34
  • @PaulSamsotha Thanks for your comment. I've edited my post and hope it would be more clear right now. – Far Zad Sep 27 '18 at 21:35
  • I've run into a similar problem where spring doesn't bind to localhost sometimes. Might want to make sure you're either listening on 0.0.0.0:8990 or 127.0.0.1:8990 also log into server (if you can) and try to curl the url. I may be (espe with multiple interfaces) that one process bound to a different interface. – Mainguy Sep 27 '18 at 21:41
  • Thanks @Mainguy. I'm afraid I couldn't understand what should I exactly do. Could you please explain or refer me to any related link. And do you think this is the Spring problem and not Angular (Client) one?! – Far Zad Sep 27 '18 at 21:46
  • OK, here's what I would do....from some other machine do: curl -vvv http://getuserid.mycompany.com:8990/getUserId and curl -vvv http://getuserid.mycompany.com:4200/app-component (or whatever the route is...I'm not an angular guru)... if both of those work and you don't get a connection error if you get any sort of error, post the output here. – Mainguy Sep 27 '18 at 21:54
  • @Mainguy Thanks, I've done that. Both of them gave me the response successfully. 4200 response `HTTP/1.1 200 OK` and 8990 `HTTP/1.1 405` with message `Method Not Allowed`. Also as I mentioned when I call the 8990 by same Angular project from my local machine it send request and receive response successfully does it mean the 8990 doesn't have any issue? or it means it only accept my local machine request?! – Far Zad Sep 27 '18 at 22:24
  • I found the answer. Thanks to @Mainguy because of clue. But they put my post on hold!!! so I cannot answer it. Does anyone know what should I do to put my post back from on hold! to normal, and who is going to do that? cheers – Far Zad Sep 27 '18 at 23:25
  • You need to look at the browser developer tools. If you are going to be developing frontend apps, you _MUST_ learn to use this. There you can see the requests and find out more about what's happening. You will see any exceptions occurring on the client side along with many other things. Just saying that "nothing happens" doesn't help us. There is so much going on under the hood that will offer more of an explanation as to what the problem is. And a lot of that can be discovered in the developer tools. Please take some time to learn it. – Paul Samsotha Sep 28 '18 at 00:45
  • In the developer tools (Use Chrome), look in the Network tab, and when you click the button you will see a new item added to the list of network requests. This item is the API request. If you click it, you can see more information about the request. – Paul Samsotha Sep 28 '18 at 00:49
  • @PaulSamsotha Thanks. I usually use fiddler to monitor request and response. I also use those stuff in browser (console, network, ...) but mostly on designing scope. so it would be good point to use them for network stuff as well and thanks to remind me. Anyway, I realized the problem was the Port numbers which was filtered by our Security Department. I chose 8080 for angular app and now everything works fine. – Far Zad Sep 28 '18 at 01:41

1 Answers1

0

The problem is the port number 4200 which was filtered by Network Department of company. After changing the port to 8080 it works well.

Far Zad
  • 389
  • 4
  • 11