How to pass data paramaters via AJAX in an ASP.NET WebService using jQuery
Everything works fine when I run the method without including parameters
, but whe开发者_StackOverflown the method is run with parameters
added, I get a 500 Internal Server Error
. I am not sure what I am doing wrong, thanks for any help.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Included below is the code that I am currently using:
[WebMethod]
public static string UploadNewImage(string filePath,string ImageTitle,string ImageDescription,string ImageKeywords)
{
}
var parameters = "{'filePath':'" + fileuploadpathValue.val() + "','ImageTitle':'" +
titleValue.val() + "','ImageDescription':'" + descriptionValue.val() + "','ImageKeywords:'" +
keywordsValue.val() + "'}";
$.ajax({
type: "POST",
url: "../MainService.asmx/UploadNewImage",
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
change your parameters to as follows:
var parameters = {
filePath: fileuploadpathValue.val(),
ImageTitle:titleValue.val(),
ImageDescription:descriptionValue.val(),
ImageKeywords:keywordsValue.val()
};
or combine them as follows:
$.ajax({
type: "POST",
url: "../MainService.asmx/UploadNewImage",
contentType: "application/json; charset=utf-8",
data: {
filePath: fileuploadpathValue.val(),
ImageTitle:titleValue.val(),
ImageDescription:descriptionValue.val(),
ImageKeywords:keywordsValue.val()
},
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
also make sure that none of the val() here is null that is you dint have a value set on any of the above control if that is the csae you will get an error like "Null passed into parameter which does not accept null values"
The problem is in your Json format,
This is working in my system,
var parameters = '{"filePath": "'+fileuploadpathValue.val()+'","ImageTitle": "'+titleValue.val()+'","ImageDescription": "'+descriptionValue.val()+'","ImageKeywords": "'+keywordsValue.val()+'"}';
$.ajax({
type: "POST",
url: "../MainService.asmx/UploadNewImage",
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
Let me know if u need more in that
精彩评论