Why are new lines not working in this javascript alert window? Why are new lines not working in this javascript alert window? ajax ajax

Why are new lines not working in this javascript alert window?


If your console log is showing \n rather than a new line, then it means the \ is escaped in the original string...

Compare

console.log(1,"\\n\\n");console.log(2,"\n\n");

Solution

use .replace() to swap your \n with newline characters

console.log(3,"\\n\\n".replace(/\\n/g,"\n"))


The most likely cause of this is that your PHP code is escaping the backslash by adding another backslash in front of it. By the time it gets to your JS code, it's actually

"Username is required\\nPassword is required..."

You can inspect the raw response in the network panel of your debugger. If you try to view it in the console, it'll display the formatted output instead of the raw output.

Double-check your method of JSON serialization in your PHP code and make sure it's doing what you expect with the \n.


try adding a space after \n .It should work