简体   繁体   中英

Javascript setting form attributes

Javascript code:

<script type="text/javascript">
   function submitForm() {
    alert("hhhhh");

//            document.forms[0].action ="http://navislink.apmtmumbai.com/express/lines/cnt_details.jsp";
//            document.forms[0].submit();
       $.ajax({
        type: "POST",
        url: "index.aspx/GetCurrentTime",
        data: '{name: " + rashmi + " }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success: function (data, status) {

                console.log("CallWM");
                alert(data.d);
            },               
            failure: function (data) {
                alert(data.d);
            },
            error: function (data) {
                alert(data.d);
            }
        });

    }

function OnSuccess(response) {
    alert(response)
    document.forms[0].action =response;  //"http://navislink.apmtmumbai.com/express/lines/cnt_details.jsp";
    document.forms[0].submit();
}

webmethod

 [System.Web.Services.WebMethod]
    public static string GetCurrentTime(string name)
    {
        //return "Hello " + name + Environment.NewLine + "The Current Time is: "
        //+ DateTime.Now.ToString();
        DataTable dtContTrack = new DataTable();
        dtContTrack = Class1.GetRecord1("SELECT u_url FROM urltb WHERE u_id=(  SELECT max(u_id) FROM urltb  )");
        return dtContTrack.Rows[0]["u_url"].ToString();
    }

Here is my brief code,i want set some link to the form action.on button click submitForm() is executing,if hard code the code it will work;if i use in onsuccess method the url is not setting.Please help me to resolve.

esponse is the object always. In order to to get your data you have to use response.d.

Source:- http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/

“.d” what? If you aren't familiar with the “.d” I'm referring to, it is simply a security feature that Microsoft added in ASP.NET 3.5's version of ASP.NET AJAX. By encapsulating the JSON response within a parent object, the framework helps protect against a particularly nasty XSS vulnerability.

You need to write response.d as below :-

function OnSuccess(response) {
    alert(response.d)
    document.forms[0].action =response.d;  //"http://navislink.apmtmumbai.com/express/lines/cnt_details.jsp";
    document.forms[0].submit();
}

This will work-->

function OnSuccess(response) {
var TestURL=response.replace(/"/g, '');
        alert(TestURL);
        document.forms[0].action =TestURL;
            document.forms[0].submit();
    }

Replace the quotes around the URL string with blank spaces :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM