0x800a139e - JavaScript运行时错误:语法错误,无法识别的表达式:#<%= fieldGenieConnectionString.ClientID%>

时间:2015-11-10 10:10:50

标签: javascript ajax

请参阅以下代码:

function GroupUSNChange() {
        //$.ajax({
        //    type: "POST",
        //    contentType: "application/json; charset=utf-8",
        //    dataType: "html",
        //    success: function OnSuccess(response) {
        var frm = document.forms[0];
        var usn = document.getElementById("ctl00_ContentPlaceHolder1_hfUSN");


        if (checkboxticked())
        {
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox" && frm.elements[i].name.substr(0, 38) == "ctl00$ContentPlaceHolder1$RematchGroup") {
                    if (frm.elements[i].checked == true) {
                        var id = frm.elements[i].name.split("|");
                        $.ajax({
                            type: "POST",
                            url: "frmPNList.aspx/ChangeGroupOfUSNs",
                            async: false,
                            contentType: "application/json; charset=utf-8",
                            data: '{strNewUSN: "' + usn.value + '", strURNs: "' + id[1] + '", strDatasetName: "' + id[2] + '", strCon: "' + $("#<%=fieldGenieConnectionString.ClientID%>")[0].value + '"}',
                        //data: '{strNewUSN: 9, strURNs: 1, strDatasetName: 2}',
                        dataType: "json",
                        success: OnSuccess(),
                        error: function (xhr, errorType, exception) {
                            var errorMessage = exception || xhr.statusText; //If exception null, then default to xhr.statusText  
                            alert("there was an error changing the USN of the group: " + errorMessage);
                        },
                        failure: function () {
                            alert('there was an error changing the USN of the group.')

                        }
                    });

                    function OnSuccess() {
                        return function () {

                        }
                    }
                    //end of AJAX call
                }
            }
        }
        alert('Successfully changed the USN of the group')
            //},


            //error: function (xhr, errorType, exception) {
            //    var errorMessage = exception || xhr.statusText; //If exception null, then default to xhr.statusText  
            //    alert("there was an error creating a screenshot (part one): " + errorMessage);
            //},
            //failure: function (reponse) {
            //    alert('there was a problem creating a screenshot.')

            //}
            //});
        }
        else
        {
            alert("You must tick at least one tickbox"); 
        }


    }

当我将它包含在.aspx文件中时,代码可以正常工作。但是,当我创建.js文件并引用.js文件时;我收到以下错误提示:

1 个答案:

答案 0 :(得分:1)

您正在将JS(或其他服务器端代码)放入JS中。

如果您的JS位于服务器作为ASP.NET处理的文件中,那就没问题(只要数据被正确转义或不包含特殊字符)。

当你把它放在一个静态的JS文件中时,服务器不再将它作为ASP.NET处理并将代码发送到客户端......它试图以JS的形式执行它并失败。

或者:

  • 将数据存储在HTML中并从DOM中读取
  • 从ASP.NET生成您的JS文件(确保正确设置Content-Type响应标头)。
相关问题