This is a migrated thread and some comments may be shown as answers.

Sending Data to Server using Express(Node.js)

1 Answer 132 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sumit
Top achievements
Rank 1
Sumit asked on 06 Oct 2016, 06:05 AM

I am new in angular2-Nativescript when i am sending data to server which is in Express(Node.js). Some function are able to send data to server but some function is not working.

Working Code

login(user: User) {
    let headers = new Headers();
    headers.append("Content-Type", "application/json");
    return this._http.post(
      Config.apiUrl + "login",
      JSON.stringify({
        email: user.email,
        password: user.password
      }),
      { headers: headers }
    )
      .map(response => response.json())
      .map((response) => {
        if (response.authentication == true) {
          alert('Login Successful');
          console.log("returning result = ", JSON.stringify(response.result));
          console.log("response == ", JSON.stringify(response.result));
          localStorage.setItem('userInfo1', JSON.stringify(response.result));
        } else {
          alert(response.message);
        }
      })
      .catch(this.handleErrors);
  }

 

Not Working Code

verify(user: User) {
    console.log(JSON.stringify({ user }));
    let headers = new Headers();
    headers.append("Content-Type", "application/json");
    return this._http.post(
      Config.apiUrl + "verify",
      JSON.stringify({
        verificationCode: user.verificationCode
      }),
      { headers: headers }
    )
      .map(response => response.json())
      .map((response) => { })
      .catch(this.handleErrors);
  }

I am facing this issue in ios

1 Answer, 1 is accepted

Sort by
0
Nick Iliev
Telerik team
answered on 06 Oct 2016, 06:34 AM
Hello Sumit,

I have examed your code, still without a detail on what is the output behaviour I can not guide you to a reasonable solution. We will need the error you are receiving or/and the behaviour you are expecting and what is really happening after you try to execute the verify function in your application.

However, there is some possible reason for your code not to work in iOS (of course, the following are just a suggestions).

One thing is that on iOS you can not make the request to HTTP but by default only to HTTPS. You will receive transport protocol security error if you try that. Another possible issue is when you are receiving the response as JSON you are not actually working with it.
response) => { }
Is that the expected behaviour!? 
Or maybe you need to log the response and further parse the response data like this

    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username: this.user , password: this.pass })
}).then(response => {
    return response.json();
}).then(res => {
    var jsonObjectBody = JSON.parse(res.data); // parse the data need to JSON
    this.message = "Data successfully send by user " + jsonObjectBody.username;
}).catch(err => {
    // Error
});  

We will wait for your error log and detailed description the issue to help you out further.

Regards,
Nikolay Iliev
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussion
Asked by
Sumit
Top achievements
Rank 1
Answers by
Nick Iliev
Telerik team
Share this question
or