Web service - Response with status: 0 for URL: null

Hello,
by consuming a web service REST, I encountered this new error message :

Response with status: 0 for URL: null

Here you have the links to my codes :
app.module.ts
home-service.ts
home.ts

So, I understood my problem was related to the CORS thing, so, by reading this page, I ran the npm install cors command, I got these messages :

npm WARN saveError ENOENT: no such file or directory, open ‘C:\Windows\system32
package.json’ npm WARN enoent ENOENT: no such file or directory, open 'C:\Windows\system32\package.json’
npm WARN system32 No description
npm WARN system32 No repository field.
npm WARN system32 No README data
npm WARN system32 No license field.

  • cors@2.8.4
    updated 1 package in 5.785s

Strangely, the C:\Windows\system32\package.json file didn’t exist and I found the file C:\Windows\system32\package-lock.json, so, I made a copy of this file and i renamed it into package.json.

By running then the npm install cors command, these messages appeared :

npm WARN system32 No description
npm WARN system32 No repository field.
npm WARN system32 No license field.

  • cors@2.8.4
    removed 1 package and updated 1 package in 4.363s

Maybe it’s useless but here you have the content of my package-lock.json file:

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "@types/node": {
      "integrity": "sha512-R+LdMJHJQwRd/Ca0Nr5KnwbSWHxTD3DWz4ivqoPeNH+YPcuirMWK+Ti9Mx32jOecmPhHOCd+6CefU5e1eVq2Ew==",
      "resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.31.tgz",
      "version": "8.0.31"
    },
    "cors": "^2.8.4",
    "object-assign": {
      "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=",
      "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
      "version": "4.1.1"
    },
    "vary": {
      "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=",
      "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
      "version": "1.1.2"
    }
  }
}

What else should I do to consume my web service? The web service I’m consuming runs on postman, I even took a screenshot of it.

Thanks

If you are running the project in browser, then you can use chrome as a browser and install CORS extension and you are ready to go.

1 Like

This is a CORS issue. This happens when you try to access different IP adress from another website running on another IP.
In chrome you can add CORS plugin as @nirav_ionic mentioned.

After installation, you can enable it direclty. But, few websites(Gmail, google hangout) won’t work.
In that case, add the IP you are accessing to get data as pattern in CORS plugin you just installed.
Eg - *://RAN.DOM.IP.ADD/*(replace with proper IP address) or *://127.0.0.1/*(localhost IP address(no need to add this)).

In application which directly access designated IP, you don’t need CORS plugin in that case.

1 Like

Many thanks, that works!!
Strangely, I don’t manage to parse the returned value, which is a JSON object, I will continue to search

It could be string of JSON object instead of normal object.
You should try json.parse(data).

response:any;

let options = new RequestOptions({headers: myHeaders});
	this.http.get(url_connection,options)
	.subscribe(
		data =>{
                  response=data;
                  console.log("Nom = ",response.result.Nom);
                },
		err=>alert(err)
		);

With this code :

	let options = new RequestOptions({headers: myHeaders}); 
	this.http.get(url_connection,options) 
	.subscribe(
		data  => {this.data=JSON.stringify(data); 
				let obj=JSON.parse(this.data); 
				let obj2=obj[Object.keys(obj)[0]];
				alert(obj2);


				},
		err=>alert(err)
		);

My obj2 object has this value :

{
    "result": {
        "Identifiant": "7B659A22DF8901EB003400000711001F",
        "Nom": "DALAGLIO",
        "Prenom": "VERONIQUE",
        "AncienneteGroupe": "7 années, 8 mois  et 26 jours",
        "Adresse": "400 CHEMIN DES ECOLIERS",
        "Ville": "PAU",
        "CodePostal": "64000"
    }
}

And it seems that obj2 got turned into a string object, because of these results :
obj2[0]={
obj2[1]="
obj2[2]=r
obj2[3]=e

And I would need to get the value of the Identifiant key.

EDIT : problem solved with this code :

let obj3=null;
eval("obj3 = ("+obj2+")");
alert(obj3.result.Identifiant);
  • Do not use JSON.stringify.
response:any;


	let options = new RequestOptions({headers: myHeaders}); 
	this.http.get(url_connection,options) 
	.subscribe(
		data  => {
                this.response=data; 
				console.log("Identifiant= ",response.result.Identifiant);
				},
		err=>alert(err)
		);

doesn’t work, I get this error message :
Runtime Error
Cannot read property ‘Identifiant’ of undefined

EDIT: data has this returned value :
{"_body":"{“result”:{“Identifiant”:“7B659A22DF8901EB003400000711001F”,“Nom”:“DALAGLIO”,“Prenom”:“VERONIQUE”,“AncienneteGroupe”:“7 années, 8 mois et 26 jours”,“Adresse”:“400 CHEMIN DES ECOLIERS”,“Ville”:“PAU”,“CodePostal”:“64000”}}",“status”:200,“ok”:true,“statusText”:“OK”,“headers”:{“content-type”:[“application/json; charset=utf-8”]},“type”:2,“url”:“http://win-9u040a2kpbv:8081/sdata/WSMobiliteAdeleRH/DonnerConnexion?aLogin=DALA%40GMT-CONSEIL.FR&aMDP=DALAGLIO”}

@GMTConseil

send me this whoule console

data  => {
           this.response=data; 
	       console.log("response= ",response);
		},

[17:36:16] console.log: Angular is running in the development mode. Call enable
ProdMode() to enable the production mode.
[17:36:16] console.warn: Native: tried calling StatusBar.styleDefault, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
[17:36:16] console.warn: Native: tried calling SplashScreen.hide, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
[17:36:18] console.log: ionViewDidLoad PagePersonnellePage
[17:36:19] console.error: ERROR
[object Object]

try below for run, the Cordova json add in your project.

> ionic cordova run browser

that works until I closed my browser (chrome) because of the auto compilation which wasn’t available, I ran then my project with the ionic serve line code, I still got the error message as before, I ran then with ionic cordova run browser, the error message is now back, I’m confused, why does my code sometimes work?

I just published an issue on my github account, what’s the best method to inform the ionic administrators?

Hello again everyone.
My problem got solved until the version 3.15.1 was released, now with the version 3.15.2, the error message Response with status: 0 for URL: null is now back, what happened and how can I solve this problem? On google chrome, even if I disable and then enable the allow control allow origin application, I keep having the same error message :confused:
EDIT: never mind, that works now, I had to run the service of my web service, that’s all

same issue i have face when i add 3 or more value in header.
when i add new key(x-auth-token) in header and send to server it’s show me

if (token != ‘’){
this.global.headerData[‘x-auth-token’] = token;
this.global.headerData[‘x-access-token’] = token; // success : response come from server
}

   let headers = new Headers(this.global.headerData);
        let options = new RequestOptions({ headers: headers });
        this.http.get(url, options).retry(2).timeout((timeoutOption == 'default') ? this.global.defaultTimeout:this.global.customTimeout)
          .map(res => {});