1

Javascript code:

...............
...............
var cutid = $(th).attr("data-cutid");

var request = $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Services/Cut.asmx/CheckCuts",
    data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
    dataType: "json"
}).responseText;

alert(request); // undefined

Function from web service:

    [WebMethod]        
    public string CheckCuts(List<CutM> cuts, Guid idCut)
    {
        return UtilCut.CheckCuts(cuts, idCut).ToString();
    }

The responseText is undefined. Why?


I added async: false to ajax request. Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called.

This code works:

function AjaxCheckCuts(ListCuts,cutid) 
{
    var request = $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Services/Cut.asmx/CheckCuts",
    async: false,
    data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
    dataType: "json"       
    }).responseText;

    var r = jQuery.parseJSON(request);
    r = r.d;
    return r;
}
POIR
  • 3,110
  • 9
  • 32
  • 48

1 Answers1

3

Is the web service working correctly? Is it returning a HTTP 200? Can you see the data that is being returned using F12 tools or Fiddler?

$.ajax() is returning a deferred. Define a done method for it to execute when the async call completes. There is no responseText property, that's why it is returning a undefined.

Try this:

var cutid = $(th).attr("data-cutid");

var request = $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Services/Cut.asmx/CheckCuts",
    data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
    dataType: "json"
});

request.done(function(result){
    alert(result);
});
DaveB
  • 9,470
  • 4
  • 39
  • 66