Jquery Ajax Call always returns error
Asked Answered
O

1

0

I have a javascript code as below

                var xReg = '<region><width>' + $("#txtWidth").val() + '</width>  <height>' + $("#txtHeight").val() + '</height><float>' + $("#floats").val() + '</float><contenttype>' + $("#contenttype").val() + '</contenttype><style>' + rgnStyle + '</style></region>';
                $.ajax({
                    type: "POST",
                    url: "myurl/addRegion",
                    data: "{pubId: '" + Number($("#pubs").val()) + "',section: '" + $("#sections option:selected").text() + "',layoutW: '" + Number($("#txtLayoutW").val()) + "',layoutH: '" + Number($("#txtLayoutH").val()) + "',bSubReg: '" + Boolean($("#chkSubRegion").is(':checked')) + "',parentRegId: '" + Number(parentRgn) + "',sXmlRegion: '" + xReg.toString() + "'}",
                    contentType: "application/json; charset=utf-8",
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        document.body.style.cursor = 'pointer';
                        if (result.d == -1) {
                            $("#errMsg").text("Failed to create new region");
                        }
                        else {
                            if ($("#chkSubRegion").is(':checked')) {
                                $("#regions").append("<option class='ddindent' value='" + result.d + "'>REGION-" + result.d.toString() + "</option>");
                            } else {
                                $("#subregions").append("<option class='ddindent' value='" + result.d + "'>SUBREGION-" + result.d.toString() + "</option>");
                            }
                        }
                    },
                    error: function (result) {
                        if (result.status == 200 && result.statusText == 'OK') {
                            if ($("#chkSubRegion").is(':checked')) {
                                $("#regions").append("<option class='ddindent' value='" + result.d + "'>REGION-" + result.d.toString() + "</option>");
                            } else {
                                $("#subregions").append("<option class='ddindent' value='" + result.d + "'>SUBREGION-" + result.d.toString() + "</option>");
                            }
                        }
                        else {
                            alert("FAILED : " + result.status + ' ' + result.statusText);
                        }
                    },
                    async: true
                });

Server code as below

[WebMethod]
public int addRegion(int pubId, string section, int layoutW, int layoutH, bool bSubReg, int parentRegId, string sXmlRegion)
{
    string path = Server.MapPath("~");
    path = Path.Combine(path, "Published");
    path = Path.Combine(path, pubId.ToString());
    path = Path.Combine(path, section);
    XmlDocument doc = new XmlDocument();
    int rgnCount = 0;
    try
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        path = Path.Combine(path, "layout.xml");
        if (!File.Exists(path))
        {
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);
            XmlNode templateNode = doc.CreateElement("layout");
            doc.AppendChild(templateNode);
            XmlNode xNodeW = doc.CreateElement("width");
            xNodeW.AppendChild(doc.CreateTextNode(layoutW.ToString()));
            XmlNode xNodeH = doc.CreateElement("height");
            xNodeH.AppendChild(doc.CreateTextNode(layoutH.ToString()));
        }
        else
        {
            doc.Load(path);
            doc.DocumentElement.SelectSingleNode("/layout/width").InnerText = layoutW.ToString();
            doc.DocumentElement.SelectSingleNode("/layout/height").InnerText = layoutH.ToString();
        }
        XmlElement root = doc.DocumentElement;
        XmlNode xParent = root;
        if (bSubReg)
        {
            xParent = root.SelectSingleNode("/layout/region[id='" + parentRegId.ToString() + "']");
            rgnCount = xParent.SelectNodes("/region").Count;
        }
        else
        {
            rgnCount = root.SelectNodes("/Layout/region").Count;
        }

        rgnCount++;

        XmlDocumentFragment docFragment = doc.CreateDocumentFragment();
        docFragment.InnerXml = sXmlRegion;
        XmlNode xID = doc.CreateElement("id");
        xID.AppendChild(doc.CreateTextNode(rgnCount.ToString()));
        docFragment.FirstChild.AppendChild(xID);
        xParent.AppendChild(docFragment);


        doc.Save(path);
        return rgnCount;
    }
    catch (Exception eX)
    {
        return -1;
    }
}

The call is going to server from client. And no issues I could find in server code till last return statement while I debugged it. In the javascript debugging I found that after ajax call is always going to error callback function. Can anyone suggest whats wrong with the code.

Thanks & Appreciate Your Time.

Orb answered 24/10, 2013 at 4:35 Comment(5)
the success function of the jquery ajax depends on a successful HTTP response code from the server. In your server code I do not see it echoing any headers or text of any kind, only returning a value. Can you post to the server directly without the ajax call?Question
I am getting undefined error.Orb
Pastor, i could not use Form post method, dont know but it never triggred postback while click on html5 submit button then I decided to call web service, call was successful but throwing exception. I have other jquery ajax calls on the same page, no issues with them.Orb
one more observation is that the ajax callback :error is triggered before even web method call is triggered on server sideOrb
even after setting async: falseOrb
O
0

I found the bug in my code there is issue with below lines of code

            },
            async: true

at the end of error: callback function

I removed the line async: true and it worked

Orb answered 24/10, 2013 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.