NativeScript / nativescript-angular

Integrating NativeScript with Angular
http://docs.nativescript.org/angular/tutorial/ng-chapter-0
Apache License 2.0
1.21k stars 241 forks source link

Post request redirect to get request #1537

Closed tahir-jamil closed 5 years ago

tahir-jamil commented 6 years ago

When i send post request to php backend it will read as a get instead of post. and params are not pass to the backend i am using httpclient module as import { NativeScriptHttpClientModule } from "nativescript-angular/http-client"; hear is the post function ` private serverUrl = "http://localhost:8000/api"; postCountsData(data: any) { let options = this.createRequestOptions(); return this.http.post(this.serverUrl+'/counts', {duroodCount: 11}, {headers: options}); }

` these are my header


private createRequestHeader() {
    // set headers here e.g.
    let headers = new HttpHeaders({
      "AuthKey": "my-key",
      "AuthToken": "my-token",
      "Content-Type": "application/json",
    });
    return headers;
  }

hear i subscribe

  onButtonTap() {
    this.userService.postCountsData(this.createCounts).subscribe(res => {
      console.dir(res);
      // this.router.back()
    }, (error) => {
      console.dir(error);
    });
  }

Hear is the preview as you can see we have get method instead of post

screen shot 2018-10-03 at 10 49 55 pm

i have also try to use JSON.stringify but nothing happened some friends says i have to use server url but i don't have any server yet then how can we solve this issue

My package.json is


{
    "description": "NativeScript Application",
    "license": "SEE LICENSE IN <your-license-filename>",
    "readme": "NativeScript Application",
    "repository": "<fill-your-repository-here>",
    "nativescript": {
        "id": "org.nativescript.goshaedurood",
        "tns-ios": {
            "version": "4.2.0"
        },
        "tns-android": {
            "version": "4.2.0"
        }
    },
    "dependencies": {
        "@angular/animations": "6.1.0",
        "@angular/common": "6.1.0",
        "@angular/compiler": "6.1.0",
        "@angular/core": "6.1.0",
        "@angular/forms": "6.1.0",
        "@angular/http": "6.1.0",
        "@angular/platform-browser": "6.1.0",
        "@angular/platform-browser-dynamic": "6.1.0",
        "@angular/router": "6.1.0",
        "lodash": "^4.17.10",
        "nativescript-angular": "6.1.0",
        "nativescript-drop-down": "^4.0.1",
        "nativescript-fancyalert": "^1.2.0",
        "nativescript-geolocation": "^4.3.0",
        "nativescript-pager": "^8.0.2",
        "nativescript-swiss-army-knife": "^3.0.0",
        "nativescript-theme-core": "~1.0.4",
        "nativescript-ui-chart": "^3.7.0",
        "nativescript-ui-dataform": "^3.6.1",
        "nativescript-ui-listview": "^3.5.10",
        "nativescript-ui-sidedrawer": "^4.1.1",
        "nativescript-youtubeplayer": "^2.2.6",
        "reflect-metadata": "~0.1.8",
        "rxjs": "~6.1.0",
        "tns-core-modules": "^4.2.1",
        "zone.js": "^0.8.26"
    },
    "devDependencies": {
        "@types/lodash": "^4.14.116",
        "babel-traverse": "6.4.5",
        "babel-types": "6.4.5",
        "babylon": "6.4.5",
        "lazy": "1.0.11",
        "nativescript-dev-sass": "^1.6.0",
        "nativescript-dev-typescript": "~0.7.0",
        "typescript": "~2.7.2"
    }
}
tsonevn commented 6 years ago

Hi @tahir-jamil, I tested this post method with our SDK sample app, however, was unable to recreate the issue. For your convenience, I am attaching a GIF file. You can see that the request type is handled correctly from the server. screencast 2018-10-04 at 2 21 46 pm

Can you check if the backend handles the request correctly?

tahir-jamil commented 6 years ago

ya i have also studied on it and found that its not working in case of localhost but for server url its working

tahir-jamil commented 6 years ago

i am using lumen api in laravel and use the server of laravel so my db is not in the xampp or mamp local server and thats why i am unable to access my server using my notebook id

tahir-jamil commented 6 years ago

i am not good in db and backend services also so for now i am unable to run laravel lumen on xampp or mamp i am working on it so that it might work

tahir-jamil commented 6 years ago

but for server url its works perfectly fine as post

tahir-jamil commented 6 years ago

do you have test with localhost with php backend?

tahir-jamil commented 6 years ago

@tsonevn well i have tested it and found thats its the issue of my database not the http its the laravel issue so i think its done for angular side

tahir-jamil commented 6 years ago

i have almost find the issue but it is unclear yet , wait i will describe you about it when i request the data from http as a post request its in json formate and the header are www urlencoded so lumen does not received the params

if i force http to send data as www urlencoded like

  let body = new URLSearchParams();
    body.set('duroodCount', "tahir");
    body.set('party_id', "23");

force to send data in key value pair then the data is successfully post in the db but the strange this is that my data is saved in the database and the network in that case is the same Get request also in network their are no params its strange but my data is posted in the mysql db

tsonevn commented 5 years ago

Hi @tahir-jamil, Did you find, what is causing the issue with the HTTP request and whether it is a problem with NativeScript? Also, keep in mind that in NativeScript Angular we are using @angular/common/http module provided by Angular. Regarding that, the issue might be related to a problem in the Angular's module.

tahir-jamil commented 5 years ago

well did not get any cause for that right know

tsonevn commented 5 years ago

Hi @tahir-jamil, I think we can close this issue for now. I will reopen it when we have stable steps to reproduce, which can be used for debugging and show that the problem is related to NativeScript

tahir-jamil commented 5 years ago

ok no problem