我有以下jquery代码来调用aspx页面中的webmethod
$.ajax({
type: "POST",
url: "popup.aspx/GetJewellerAssets",
contentType: "application/json; charset=utf-8",
data: '{"jewellerId":' + filter + '}',
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
Run Code Online (Sandbox Code Playgroud)
这是web方法签名
[WebMethod]
public static string GetJewellerAssets(int jewellerId)
{
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是现在我需要将两个参数传递给web方法
新的Web方法看起来像这样
[WebMethod]
public static string GetJewellerAssets(int jewellerId, string locale)
{
}
Run Code Online (Sandbox Code Playgroud)
如何更改客户端代码以成功调用此新方法签名?
编辑:
以下2种语法有效
data: '{ "jewellerId":' + filter + ', "locale":"en" }',
Run Code Online (Sandbox Code Playgroud)
和
data: JSON.stringify({ jewellerId: filter, locale: locale }),
Run Code Online (Sandbox Code Playgroud)
其中filter和locale是局部变量
我想控制何时回复错误消息和成功消息,但我总是收到错误消息:
这是我想要做的:
$.ajax({
type: "POST",
data: formData,
url: "/Forms/GetJobData",
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
alert("success!")
},
error: function (response) {
alert("error") // I'm always get this.
}
});
Run Code Online (Sandbox Code Playgroud)
控制器:
[HttpPost]
public ActionResult GetJobData(Jobs jobData)
{
var mimeType = jobData.File.ContentType;
var isFileSupported = AllowedMimeTypes(mimeType);
if (!isFileSupported){
// Error
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content("The attached file is not supported", MediaTypeNames.Text.Plain);
}
else
{
// Success
Response.StatusCode = (int)HttpStatusCode.OK;
return Content("Message sent!", MediaTypeNames.Text.Plain);
}
}
Run Code Online (Sandbox Code Playgroud) 我有如下控制器:
public ActionResult Save(string input, string name) {
//Some code
return PartialView();
}
Run Code Online (Sandbox Code Playgroud)
我需要对此控制器方法进行ajax调用并传递两个参数input和value
我的ajax调用如下所示:
$.ajax({
url: '/Home/Save',
type: 'POST',
async: false,
dataType: 'text',
processData: false,
data: "input=" + JSON.stringify(data) + "&name =" + $("#name").val(),
success: function (data) {
}
});
Run Code Online (Sandbox Code Playgroud)
我无法将值传递给name参数.. name参数中的值变为null ..请帮助我..提前感谢