2

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.

Graham
  • 7,807
  • 20
  • 69
  • 114
  • 2
    You might find [this article](http://stackoverflow.com/questions/876561/when-serving-javascript-files-is-it-better-to-use-the-application-javascript-or) useful. You should be sending it as `Response.ContentType = "application/javascript"`. – user692942 Apr 03 '14 at 08:46
  • Yes, tried that - also did not work – Graham Apr 03 '14 at 08:48
  • 1
    Is the MIME Type registered in IIS? Maybe [this article](http://forums.iis.net/t/1195352.aspx?Javascript+error+when+application+runs+in+IIS+7+on+windows+server+2008) will help. – user692942 Apr 03 '14 at 08:49
  • No it isn't. What should I put the extension as? – Graham Apr 03 '14 at 08:52
  • When I try to add it, it says it already exists even though it is not listed – Graham Apr 03 '14 at 08:53
  • 1
    Strange not come across that maybe this will help [IIS 7.5 Error Caused by Adding mimeMap to DNN Site (Duplicate Entry for the same mimeType)](http://jingyangli.wordpress.com/2012/04/05/iis-7-5-error-caused-by-adding-mimemap-to-dnn-site-duplicate-entry-for-the-same-mimetype/) – user692942 Apr 03 '14 at 08:56
  • OK, it is the .js that already exists, which I have changed to application/javascript, restarted IIS, and still not working – Graham Apr 03 '14 at 08:58
  • 1
    Sounds like you're getting closer, unfortunately I'm out of ideas but good luck finding a solution. Maybe give the server a reboot just to be safe... – user692942 Apr 03 '14 at 09:00
  • It's not a reboot issue as it is happening consistently on many machines, but thanks for the ideas anyway. – Graham Apr 03 '14 at 09:05
  • 1
    What on many servers you mean? You having this issue on multiple IIS servers? – user692942 Apr 03 '14 at 09:06
  • No just development PCs – Graham Apr 03 '14 at 09:11
  • 1
    Aw ok, you're using IIS locally, so not a client -> server environment. – user692942 Apr 03 '14 at 09:12
  • 1
    I take it you're adding your ASP script as a ``? Could you add your HTML source to the question? – user692942 Apr 03 '14 at 09:15
  • That's a little more complex. It is a form that posts to an ASP script. But rest assured that the script tags are all present and correct. – Graham Apr 03 '14 at 09:23
  • I'm not sure we have the whole picture here. "`%>notifySuccess();<%`" closes an asp block, displays the text "notifySuccess();" client side and opens a new asp block, so what you are seeing is exactly what I would expect purely from the information you have given. Do you have client side `` before and after your asp blocks? – John Apr 03 '14 at 09:40
  • I have tried it with – Graham Apr 03 '14 at 09:43
  • I think I'm going to have to rewrite it a different way, probably as an ajax call. But thanks for all the input. – Graham Apr 03 '14 at 09:45
  • how are you using your file. If you view a .js file in a browser you will see raw javascript as text. Have you tried referencing it with a script tag on another page - ` – John Apr 03 '14 at 09:52
  • See test script that I've added to the question – Graham Apr 03 '14 at 09:54
  • 1
    I tried @John but he keeps missing the point. Been over this already. – user692942 Apr 03 '14 at 09:57
  • 1
    You're trying to generate a javascript from ASP, this is fine but you then have to add that ASP file (that returns as `Response.ContentType = "text/javascript"`) as a ` – user692942 Apr 03 '14 at 10:00
  • OK, how do I do that? – Graham Apr 03 '14 at 10:02
  • Your script tags don't go on the same page. They go on a different page (of html) with a reference to your page of asp generated js – John Apr 03 '14 at 10:06
  • So I put the – Graham Apr 03 '14 at 10:08
  • See sample first script now in question – Graham Apr 03 '14 at 10:12
  • 1
    In that instance @Graham you wouldn't return your `custom2.asp` as `Response.ContentType = "text/javascript"` you would return it as `Response.ContentType = "text/html"` and make sure that the `notifySuccess()` javascript is outputted on the page or you will get `Function Not Defined` error. – user692942 Apr 03 '14 at 10:17

2 Answers2

2

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>
    
user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    OK, it seems that this has never worked, and some other code has kicked in to cover up the tracks of any problems that have occured (until now) - you deserve an uptick on all your comments for your perserverance. – Graham Apr 03 '14 at 10:18
1

If you request a JavaScript program with a browser using a mechanism usually intended to display an HTML document (e.g. by following a link, submitting a form, or typing the URL of the JS into the address bar) then the browser will display the source code of that JavaScript program as text. This is normal behaviour.

To run the JavaScript program you need to:

  1. Create an HTML document
  2. Include a <script> element in that document
  3. Set the src attribute of that <script> to the URL of the JS program
  4. Visit the HTML document with the browser

However, in order to submit a form and then run JavaScript in the context of the current page based on the response you get then you need to:

  1. Bind an event handler (with JS) to the form's submit event
  2. Prevent the default behaviour of the form submission
  3. Use Ajax to make the HTTP request (instead of submitting the form normally)
  4. Check the response (which you should probably set to be some form of structured data instead of application code)
  5. Run the JS if the response indicates that the conditions are right for it to do so

However, your Javascript appears to just reload the existing page, so you can forget about using JS entirely and use a different approach.

  1. Submit the form as normal
  2. Process the data on the server side
  3. Issue an HTTP 302 Found response with a Location header giving the URL of the page the form appeared on
  4. Allow the browser to be redirected back to where it came from, reloading the page in the process
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335