Jquery Ajax Call总是返回错误

时间:2013-10-24 04:35:27

标签: javascript jquery jquery-callback

我有一个javascript代码如下

                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
                });

服务器代码如下

[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;
    }
}

呼叫从客户端转到服务器。在调试它时,我在服务器代码中找不到问题直到最后一个return语句。在javascript调试中我发现ajax调用后总是会出现错误回调函数。任何人都可以提出代码的错误。

谢谢&amp;欣赏你的时间。

1 个答案:

答案 0 :(得分:0)

我发现我的代码中的错误存在以下代码行的问题

            },
            async: true

错误结束时:回调函数

我删除了行async:true并且它正常工作