我已按照以下答案更新了问题。我认为我的主要困惑在于'data'参数及其在js调用中的处理方式。
C#方法
[HttpPost]
public string GetPreviewURL(string activityID)
{
try
{
var requestContext = ContextHandler.GetRequestContext(Request);
object[] arguments = new object[] { requestContext };
var sew= SumtContainer.Resolve<ICLWOrker>(typeof(ICLWOrker).FullName, arguments);
return sew.GetPreviewUrl(activityID);
}
catch (Exception e)
}
在这个JS调用中,“ POST”之后的参数我认为应该是数据,但是我很困惑如何从JSON中获取URL。
apiAccessClient = new apiClient();
var apiUrl = STRING_SITE_PREFIX + '/service/webapi/GetPreviewURL/?activityID=' + actId;
//define 'data' here?
apiAccessClient.send(STRING_USERMODE, apiUrl, "POST", data, onPreviewSuccess, onError, 0, null, false, false, true);
return;
apiAccessClient当前是在另一个js文件中设置的,该文件是安装Ajax调用的地方
$.ajax({
type: this.requestType,
url: this.apiUrl,
contentType: "application/json; charset=utf-8",
context: this,
async: isAsync,
data: JSON.stringify(flattenModel(this.parameters)),
statusCode: {
401: function () { _UTL_NavigateToTimeOutPage(); }
},
beforeSend: function (request) {
request.setRequestHeader("X--AjaxRequest", "1");
request.setRequestHeader("_SUMTC", _UTL_GetCSRFToken());
request.setRequestHeader("EmulatorUserName", _UTL_GetEmulatorUserName());
request.setRequestHeader("x--authenticationmode", "jwt");
request.setRequestHeader("sumtotaljwt", _UTL_GetJWT());
request.setRequestHeader("broker_Session_Id", _UTL_GetBrokerSessionId());
if (userMode !== null && userMode.length > 0) {
request.setRequestHeader("X-UserMode", userMode);
}
request.setRequestHeader("x-viewableuserid", viewableUserId);
if (!(isUndefinedOrNull(STRING_PROXYFORID))) {
request.setRequestHeader("x-proxyforuserid", STRING_PROXYFORID);
request.setRequestHeader("x-usermode", userMode);
}
},
success: function (data, statusCode, jqXHR) {
var contentType = jqXHR.getResponseHeader("Content-Type");
if ((jqXHR.status == 200) && contentType.toLowerCase().indexOf("text/html") < 0) {
successCallback(data, this.currentContext);
} else if ((dataReturnTypeText == true) && (jqXHR.status == 200)){
successCallback(data, this.currentContext);
}
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status != 401) {
if (errorCallback === null || errorCallback === undefined) {
this.error(jqXHR, textStatus, errorThrown);
}
else {
errorCallback(jqXHR, textStatus, errorThrown);
}
}
}
});
如何获取它,以便在新窗口中打开返回的网址?
谢谢您的时间!
答案 0 :(得分:1)
如注释中所述-JS在客户端(即在浏览器中)运行,而C#在服务器上运行。要调用您的方法,您需要使用AJAX。客户端代码应如下所示:
updater
注意:如果您不使用控制器,而是使用代码隐藏的,那么您的methot应该看起来像这样
function launchURL_Test() {
var ID="your id";
$.ajax({
type: "POST",
url: "yourcontroller/GetUrl",
contentType: "application/json;charset=utf-8",
data: "{'id':'" + ID + "'}",
dataType: "json",
success: function (data) {
// do something with the result...
},
error: function (result) {
console.log('error')
}
});
}
或者在标准MVC Controller中,您的方法应如下所示:
[WebMethod]
public static string GetUrl(string id)
{
//some code
return url;
}
最后,要使用方法中的URL打开新窗口,请更新您的Ajax成功:
[HttpPost]
public ActionResult GetUrl(string id)
{
//your code
return url;
}