0

I am trying to set up automatic JSLint tests that will grab js files from a target website, and then using Selenium run it against a Test site that will perform JSLint tests using the url of the Js file.

I am running into problems trying to test this locally, with two different localhost websites.

 $(function () {

      $.ajax({
        url: "http://code.jquery.com/jquery-latest.min.js",
        //url: "http://localhost:62338/Scripts/test.js", // url to other local site
        dataType: "text"
      }).done(function (data) {
        var myResult = JSLINT(data);
        ..
      });
    });

If I run the above code with the url pointing at the jquery url, it works fine and JSLint analyses this file. But it is breaking if I try to point it at my the js file in my other localhost site. If I view the Network Inspector in Chrome, it says the call to the script was cancelled.

Any ideas why I am not able to access the locally hosted js file in this way?

I am using an MVC4 Web Project, by the way.

Edit: I had no luck with Cors or pointing to my IP, but Dominic's answer eventually led me to this question, where I am able to disable security on Chrome. I don't like this option at all, but its just to test locally before I get all the TeamCity stuff setup, and the change is undone as soon as Chrome is restarted.

Edit2: The 2nd answer on this question also worked for me, by adding:

<httpProtocol>
      <customHeaders>
        <clear />
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>

..to the target site's web.config I was able to get this to work without having to disable security on Chrome. And as the target site is just a dummy site and is being used locally this works much better for me.

Community
  • 1
  • 1
DevDave
  • 6,221
  • 11
  • 59
  • 96

2 Answers2

1

This is probably due to the same origin policy not letting ajax calls go to localhost. This might be fixed by changing the below setting before making the call.

jQuery.support.cors = true

You might be able to get around this by changing localhost to your computer's ip.

1

JavaScript can't do a cross domain request unless specifically white-listed the calling domain, since you're doing tests there is no reason why you would want to do it that.

If you have some kind of version setup (SVN, GIT) hook that into Selenium and run your test on the source code in the repository.

You can also hook JSLint into a pre-commit hook that will reject any JS code that is not JSLinted.

Halcyon
  • 54,624
  • 10
  • 83
  • 122