[英]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();
}
這是我的簡短代碼,我想設置一些到表單動作的鏈接。在按鈕上單擊SubmitForm()正在執行,如果使用硬代碼,它將起作用;如果我在onsuccess方法中使用,則未設置url。請幫助我解決。
追求總是對象。 為了獲取數據,您必須使用response.d。
資料來源: -http : //encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/
“ .d”是什么? 如果您不熟悉我所指的“ .d”,它只是Microsoft在ASP.NET 3.5的ASP.NET AJAX版本中添加的一項安全功能。 通過將JSON響應封裝在父對象中,該框架有助於防止特別討厭的XSS漏洞。
您需要編寫response.d
如下:
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();
}
這將起作用->
function OnSuccess(response) {
var TestURL=response.replace(/"/g, '');
alert(TestURL);
document.forms[0].action =TestURL;
document.forms[0].submit();
}
用空格替換URL字符串周圍的引號:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.