-2

given below is the picture of the response I get on submitting a form with wrong email. my response image

I want to remove the word message: & curly brackets around the string from this response. I am trying the following:

$('#registerStatus').text(response.replace('message:', ''))

But it doesnt work. Can you please help?

Ghazi 360
  • 98
  • 2
  • 11
  • Is response an object? – sb_ Jun 03 '21 at 10:44
  • Wherever you are displaying `response` change it to `response.message`. – M.Hassan Nasir Jun 03 '21 at 10:51
  • Add the logs for `console.log(response)` and `typeof response`. Post the values here. – adiga Jun 03 '21 at 11:13
  • @adiga log for (response) gives {"message":"Email already exists"} & log for typeof response gives string – Ghazi 360 Jun 03 '21 at 11:23
  • 2
    You have a JSON string. You need parse it to an object before adding it to text: `var o = JSON.parse(response); $('#registerStatus').text(o.message)` – adiga Jun 03 '21 at 11:27
  • You can close it as a duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383) – adiga Jun 03 '21 at 11:34

1 Answers1

3

The response is a JSON Object, so you can write:

$('#registerStatus').text(response.message)
Idan
  • 202
  • 2
  • 5