I cannot upload the . json file

Asked

Viewed 51 times

0

I’m trying to send a file .json created within Angular for a server, and gives this error as a response:

"A Jsonobject text must Begin with '{' at 1 [Character 2 line 1]", object_or_array: "Object", error_info: "This error came from the org.json Reference parser." , validate: false}"

It’s just going to the URL, without the JSON, by the way. Below is the code, made in angular:

var json={
      usuario : this.nome,
      senha : this.senha
  };

  var myJson=JSON.stringify(json);


  console.log(json);

  return this.http.post(this.url,JSON.stringify(json))
  .map(res=> res.json())
  .subscribe(data =>{
    console.log(data);
});
  • If I’m not mistaken, you don’t need to string json to function http.post, just passing the object straight.

  • Like this: ---Return this.http.post(this.url,JSON.stringify({user : this.name, password : this.password}))---? I tried and gave another error

  • No, just replacing JSON.stringify(json) for json

  • No need to convert to json, and please post the code instead of image https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions

  • i replace JSON.stringify(json) with just json and keep giving the same error. I would have a way to see the output json from the browser console?

1 answer

1


I solved the problem, just add in the webservice inside your php the following codes:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.