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 ?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 ?
It seems that $.('#id').val(' ') contains a space. So it will trigger return "success". Have you try remove the return statement like here ?
My last comment will not help a lot sorry. Take a look google chrome fire twice it could be a cache problem load the page in privacy mode. don't forget to valid the answer or update the title
Don't worry about it.... I guess I missed something but $.('#id').val('') is finally behaving exactly as it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.