Ajax post error

By : cagin
Source: Stackoverflow.com

I want to post data to a web service with ajax. there is my ajax code:

 function Looping() {
        var Grid = document.getElementById("<%= gvHastalar.ClientID %>");
        var Row;
        var Cell;
        if (Grid.rows.length > 2) {
            for (i = 1; i < Grid.rows.length - 1; i++) {
                Row = Grid.rows[i];
                Cell = Row.cells[3];
                var html = $.ajax(
                    type: "POST",
                    url: "http://localhost:7753/HastaTahlilUyariServisi.asmx/f_HastaninAktarilacakAlislabTestleri",
                    data: "{_sTcKimlikNo:" + Cell.innerHTML + ",_iKlinikKodu:18001,_bAy:12,_iYil:2009}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: alert('success'),
                    error: alert('error')
                Row.style.backgroundColor = "#D3EFD1";

And my webservice function's code is here:

    [SoapHeader("_ticket", Direction = SoapHeaderDirection.In)]//SoapHeaderDirection.Out
    public DataSet f_HastaninAlisLabTahlilleri(string _sTcKimlikNo, int _iKlinikKodu, byte _bAy, int _iYil)
            string QSelect =
                               @"SELECT * FROM [V_EUCLID_SONUC]
                                 WHERE  MONTH(KAYITTARIHI) = " + _bAy + @"
                                   AND YEAR(KAYITTARIHI) = " + _iYil +
                                   AND TCKIMLIKNO = '" + _sTcKimlikNo + @"'";

            return dbA.ExecuteDataSet(CommandType.Text, QSelect);
        catch (Exception ex)
            throw (ex);

There is a break point on function which is in the web service but debug never go that break point. I pasted webservice's url from browser but may be url is wrong. And when i run project, i have 3 alert. First Cell's text its normal.Second alert is success and the last alert is error. I want to send parameters to f_HastaninAlisLabTahlilleri and user return dataset. How can i do this?

Thanks in advance

By : cagin


You cannot put a break-point in the web-service code i.e. even the IDE would not let u debug the web-service code.... it is an old legacy the VS.Net IDE has since its inception... lets see if it is resolved in VS 2010.

By : HotTester

The url that you have specified in the JQuery script is not equal to the name of the function in c# code. Isn't it the point. *f_HastaninAktarilacakAlislabTestleri* in url and *f_HastaninAlisLabTahlilleri* in c# code. Some reasons for such a problem can be wrong url or deference between argument list of client request and argument list of the server side method or action.

Here are a few remarks about your code:

  1. success and error are callback functions, they should be defined like this:

    success: function(data) { alert('success'); },
    error: function(XMLHttpRequest, textStatus, errorThrown) { alert('error'); }
  2. ASMX web services use SOAP by default unless you decorate them with ScriptServiceAttribute in which case JSON could be used to invoke a method. It is not clear from your code if the web service is decorated with this attribute.
  3. When you pass parameters, you need to encode them, use JSON. stringify instead of concatenating strings:

    data: JSON.stringify({_sTcKimlikNo: Cell.innerHTML, 
                          _iKlinikKodu: 18001,
                          _bAy: 12,_iYil: 2009});

Use FireBug to inspect network AJAX requests and server responses and post them on StackOverflow to facilitate debugging.

This video can help you solving your question :)
By: admin