-1

I'm trying to get the class below to work, however I continue to get an error message stating

alert!parsererrorSyntaxError: Unexpected token < in JSON at position 0/Home/onPremTest{"msg":"onPremTest message"}

It appears the opening symbol in the script closing tag is causing this message. Does anyone know how to format it so that the browser accepts the javascript code and executes it?

public class Javascript {
  static string scriptTag = "<script type=\"\" language=\"\">{0}</script>";
  public static void ConsoleLog(string message) {
    string function = "console.log('{0}');";
    string log = string.Format(GenerateCodeFromFunction(function), message);
    //System.Web.HttpContext.Current.Response.Write("<script>alert('Test message !'); </script>");
    System.Web.HttpContext.Current.Response.Write(log);    
  }

  public static void ConsoleError(string message) {
    string function = "console.error('{0}');";
    string log = string.Format(GenerateCodeFromFunction(function), message);
    Page page = HttpContext.Current.Handler as Page;
    if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) {
      ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "error", "console.error('" + message + "')", true);
    } else {
      HttpContext.Current.Response.Write(log);
    }
  }

  public static void Alert(string message) {
    string function = "alert('{0}');";
    string log = string.Format(GenerateCodeFromFunction(function), message);
    Page page = HttpContext.Current.Handler as Page;
    if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) {
      ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alert", "alert('" + message + "')", true);
    } else {
      HttpContext.Current.Response.Write(log);
    }
  }

  static string GenerateCodeFromFunction(string function) {
    return string.Format(scriptTag, function);
  }
}
nAviD
  • 2,784
  • 1
  • 33
  • 54
Bbb
  • 517
  • 6
  • 27
  • The two errors are not the same, despite both complaining about <. Read the error message again. Hint: `in JSON`. – Ian Kemp Oct 29 '20 at 17:20
  • I removed the text then, thanks. Can JSON not have angle brackets? Also, how can I write to the browser console from MVC? – Bbb Oct 29 '20 at 17:54
  • can you clarify a bit more what sort of interaction you expect? is it a one-off `console.log` or you want to be able to constantly push events via the likes of [`SignalR`](https://learn.microsoft.com/en-us/aspnet/core/signalr/diagnostics?view=aspnetcore-3.1#javascript-client-logging)? – timur Nov 04 '20 at 23:27
  • I had almost called this a duplicate of https://stackoverflow.com/questions/14713782, but then I looked at the code, it's a copy of one of the answers, maybe the original author would know? –  Nov 08 '20 at 20:05

2 Answers2

1

That error means there is a problem with one of your opening tags. It might be something else too, but this is what I have experienced so far.

// this create that error.
<div something </div>

// so add ">" after div
<div> something </div>
Bulent
  • 3,307
  • 1
  • 14
  • 22
0

The function in GenerateCodeFromFunction should either use ' or " for strings(" is prefered). So I slightly changed all of your methods :

 public class Javascript
    {
        static string scriptTag = "<script type='javascript' >{0}</script>";
        public static void ConsoleLog(string message)
        {
            string function = "console.log(\"{0}\");";
            string log = string.Format(GenerateCodeFromFunction(function), message);
          
            System.Web.HttpContext.Current.Response.Write(log);
        }

        public static void ConsoleError(string message)
        {
            string function = "console.error(\"{0}\");";
            string log = string.Format(GenerateCodeFromFunction(function), message);
            Page page = HttpContext.Current.Handler as Page;
            if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "error", "console.error('" + message + "')", true);
            }
            else
            {
                HttpContext.Current.Response.Write(log);
            }
        }

        public static void Alert(string message)
        {
            string function = "alert(\"{0}\");";
            string log = string.Format(GenerateCodeFromFunction(function), message);
            Page page = HttpContext.Current.Handler as Page;
            if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alert", "alert('" + message + "')", true);
            }
            else
            {
                HttpContext.Current.Response.Write(log);
            }
        }

        static string GenerateCodeFromFunction(string function)
        {
            return string.Format(scriptTag, function.Replace("\"","'"));
        }
    }
}
nAviD
  • 2,784
  • 1
  • 33
  • 54