0

I'm using google chrome for my work.

Following https://stackoverflow.com/a/26003200/7204173 I tried to return an exception from webmethod. But it does not show anything in the console.log. The only thing I got is from network tab the generic error message below :

"An error occurred while updating the entries. See the inner exception for details."

So what I tried is below (ps : I'm sending DML a database using entity framework DbContext) :

public string AddData(myEntity e)
{
   using(var context = new MyEntityModel())
   {
       try
       {
           context.myEntity.Add(e);
           context.SaveChanges();
           return "success";
       }catch(Exception ex)
       {
          return "error : " + ex.Message;
       }

   }
}

Now my Javascript code :

function AddArticle()
{
    var e {id : "1", value : "test"};
    var DTO = { 'e': e };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(DTO),
        url: "MyPage.aspx/AddData",
        success: function (response) {
            console.log(response);            
        },
        error: function (xhr, status, error) {
            var exception = JSON.parse(xhr.responseText);            
            alert(exception.Message);
            console.log(exception.Message);
        }
    });
}

So let's say I put it a call a the javascript function behind an input button. Since it is a database, the var e {id : "1", value : "test"}; can't be added twice since id is a primary key. Still I get nothing in the console.log ; the alert box does not even get triggered. But when it succeeds, the console.log displays success as expected.

Any idea of where I shoud be looking for to find my error ?

Community
  • 1
  • 1
Kurt Miller
  • 567
  • 1
  • 8
  • 23

1 Answers1

1

Have you try to log the others errors args ?

console.log(xhr);
console.log(status);
console.log(error);

You should be able to parse manually logs in the console and find the right path to the variable you want.

Update

console.log(error); was the answer in the case of OP

Kurt Miller
  • 567
  • 1
  • 8
  • 23
Keylies
  • 32
  • 4
  • Cool. `console.log(error);` shows the error message. Thank you. BUT I also see that that both `success` and `error` are triggered. For instance adding `$.('#id').val(' ')` in the `success` is also triggered...Why ? – Kurt Miller Apr 06 '17 at 10:55
  • It seems that `$.('#id').val(' ')` contains a space. So it will trigger `return "success"`. Have you try remove the return statement like [here](http://stackoverflow.com/a/2373663/7823445) ? – Keylies Apr 06 '17 at 11:10
  • My last comment will not help a lot sorry. Take a look [google chrome fire twice](http://stackoverflow.com/questions/35061361/javascript-jquery-on-click-confirm-fires-twice) it could be a cache problem load the page in privacy mode. don't forget to valid the answer or update the title – Keylies Apr 06 '17 at 11:24
  • Don't worry about it.... I guess I missed something but `$.('#id').val('')` is finally behaving exactly as it should. – Kurt Miller Apr 06 '17 at 13:16