Search code examples
asp-classicjscript

Response.ContentType="text/javascript" not working


Can I get possible reasons why Response.ContentType="text/javascript"; would not work.

In more detail, I have:

Response.Clear();
Response.ContentType = "text/javascript";
%>notifySuccess();<%

What I am getting is just a page with the text notifySuccess(); on it. Viewing the source of the page shows just the same text.

Viewing the response headers, I get:

Cache-Control: private
Content-Type: text/javascript
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Thu, 03 Apr 2014 08:33:01 GMT
Content-Length: 149

200 OK

notifySuccess is a function in the calling page and just does a reload:

document.location.reload();

Can anyone point me in the right direction as to why this isn't working.

I'm using IIS 7.5 on windows 7, but getting the same problem on windows 2008.

After many comments with no success, I have created a test page (which has the same problem). Here is the entire test script:

<%@Language="JScript"%>
<%
Response.Clear();
Response.ContentType = "text/javascript";
%>alert(123);

Also tried:

<%@Language="JScript"%>
<%
Response.Clear();
Response.ContentType = "text/javascript";
%><script type="text/javascript">alert(123);</script>

And also:

<%@Language="JScript"%>
<%
Response.Clear();
Response.ContentType = "application/javascript";
%>alert(123);

EDIT

So with the above test script being called custom2.asp, I now have a script called custom.asp which just contains a form with an action=custom2.asp:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <form action="custom2.asp">
        <input type="submit" />
    </form>
</body>
</html>

This is basically what the existing code does, but it is in a prototype model box in the real code.


Solution

  • Right from the comments think I understand what you are missing

    There are two parts to this

    1. Your ASP page that returns as a JavaScript (for arguments sack js.asp)

      <%
        Response.Clear();
        Response.ContentType = "text/javascript";
      %>notifySuccess();
      
    2. Your ASP page that contains the JavaScript (let's call it page1.asp)

      <html>
      <head>
        <title>Really basic page</title>
      </head>
      
      <body>
        <!-- your page content here -->
        <%
          'Might have some ASP code here
        %>
        <script type="text/javascript">
          function notifySuccess() {
            // This function is defined in this page and does something.
          }
        </script>
        <!-- this will be processed as JavaScript and call notifySuccess() -->
        <script type="text/javascript" src="js.asp"></script>
      </body>
      </html>