-1

My loadView function is a somehow broken

function loadView(view){
    $.get("../content/mainView.html", function(data) {
      $("#content-container").html(data);
    })
    .done(function() {
        console.log("done");
  })
  .fail(function(xhr, status, error) {
      console.log("fail");
      console.log(error);
  });
}

I dont know what is wrong with me since I dont even can debug it since

  console.log(error);

just gives me an empty response.

enter image description here

Can someone tell me whats wrong with my code and why

console.log(error);

dont give any response?

rockZ
  • 815
  • 1
  • 12
  • 28
  • 1
    Try `console.log(arguments)` instead, and see what you get. – adeneo Aug 19 '17 at 22:47
  • 1
    A quick look at your Network tab will answer your question. The path could be wrong. And I wouldn't just output string `fail` as far as I have `xhr`. Unfortunately, there is nothing we can help with at this point. – Adam Azad Aug 19 '17 at 22:48
  • check the call in network, it'll give you a hint about whats wrong. – Abhilab Das Aug 19 '17 at 23:03
  • @AbhilabDas The Networktab stays empty since its all local. There is no call which goes over Network. It does not even run on a webserver. – rockZ Aug 19 '17 at 23:53
  • 1
    Wait what? If it doesn't run on some sort of webserver, even locally, you can't do ajax requests, as the same-origin policy would stop any request not over HTTP – adeneo Aug 20 '17 at 05:49

2 Answers2

1

It is possible in AJAX/GET request world that you may get empty error parameter in fail scenarios.

error object is the message sent from server, In case request doesnt reached till server you may get empty response.

I would recommend to use textStatus

The three parameters are explained below respectively.

.fail(function(jqXHR , textStatus , error)...... jqXHR is a JS object textStatus is "error" error is "Internal Server Error", it's the error message sent by the server.

amit wadhwani
  • 1,140
  • 1
  • 7
  • 12
0

Try using the $.ajax method instead of $.get method

function loadView(view){
    $.ajax({
         type: "GET",
         url: "../content/mainView.html", //Please put the actual url of the page
         data: {NameOfData: $("#content-container").html(data)},
         dataType: "text",
         success: function(resultData){
             //Do something
         }
    })
}

also is your code in $(document).ready(function(){})? If it isn't then also try putting your code into this function

Hope it helps!

Elisa L
  • 262
  • 1
  • 2
  • 11