0

I defined this ajax function:

function LayerPropertyService() {

    var ds = {
        getAssociatedProperties: getAssociatedProperties
    };
    return ds;

    function getAssociatedProperties(callback, error) {
        return $.ajax({
            url: '/Mobile/LayerProperty/GetAssociatedProperties',
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: { id: 4 },
            success: callback,
            error:error
        });
    }

}

Here how I call to the ajax func:

layerProp.getAssociatedProperties(function (response) {
            alert("success")
        },
        function (response) {
            alert("error")
        });

I don't know why but I get always alert("error") is popup while I don't get any errors in console and data returned from server.

Update

Here the result I get in consle:

enter image description here

Here is function in the server:

public string GetAssociatedProperties(int id)
{
    return "sssss";  
}  

As you can see it returns string.

Any idea why error function is always fired?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • 2
    Instead of `alert("error")`, do `alert(response)` - it should return the actual error message. Otherwise if you're using a browser like Chrome, open the Network tab before you run your function - it will show you the details of the call, including the request and the response. The response, in your case, will likely hold an error message telling you what went wrong. **EDIT:** I just suggested an `alert()` for debugging purposes without realizing and feel dirty for doing so. Please listen to @FelixKling and use `console.log()` instead of `alert()`. – Tyler Roper Jul 06 '17 at 17:32
  • what is the error response? – error404 Jul 06 '17 at 17:33
  • 2
    Even better, use `console.log(response)`. – Felix Kling Jul 06 '17 at 17:33
  • Are you receiving data in JSON format from server? – sagar Jul 06 '17 at 17:40
  • @sagar, I receive text – Michael Jul 06 '17 at 17:42
  • You are saying it is JSON, i am guessing you are not returning JSON – epascarello Jul 06 '17 at 17:44
  • @sagar , please see update. – Michael Jul 06 '17 at 17:46
  • A string is not JSON so if you are returning a string, than you should not be using `dataType: "json",` – epascarello Jul 06 '17 at 17:51
  • @epascarello as he said if you are using dataType: json then you must require to encode your data at server side as JSON object before returning it as response otherwise it will generate error. Example if you are echoing plain text then you first use JSON_encode to return it as response. – sagar Jul 06 '17 at 17:56

0 Answers0