Jquery Ajax Call always returns error

不羁的心 提交于 2019-12-13 05:37:53

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/19557019/jquery-ajax-call-always-returns-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!