我不知道发生了什么,但我觉得这是路由问题。
这是JSON
$("form").submit(function () {
if (api.validateForm()) {
var pid = '1198';
var image = document.getElementById("myCanvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: 'POST',
async: false,
url: 'https://localhost:44301/webapi/SaveImage/SaveSignature',
data: '{ "imageData" : "' + image + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
window.location.replace("success.aspx");
}
});
}
return false;
});
申请开始
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "webapi/{controller}/{action}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
这是我的控制器
public class SaveImageController : ApiController
{
public class CrossDomainActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
bool needCrossDomain = true;
if (needCrossDomain)
{
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
}
base.OnActionExecuted(actionExecutedContext);
}
}
[AcceptVerbs("GET", "POST")]
[CrossDomainActionFilter]
public bool SaveSignature(string imageData)
{
string uploadPath = HttpContext.Current.Server.MapPath("Files") + "\\1198\\";
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
string fileNameWitPath = uploadPath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
return true;
}
[AcceptVerbs("Get")]
public string SayHello(string aye)
{
return "Say Hello from Test Controller";
}
}
Firebug错误响应
{"Message":"No HTTP resource was found that matches the request URI 'https://localhost:44301/webapi/SaveImage/SaveSignature'.","MessageDetail":"No action was found on the controller 'SaveImage' that matches the request."}
答案 0 :(得分:0)
很可能,它是https - 您是否在https中运行您的开发环境?尝试在网址中使用http。
答案 1 :(得分:0)
Web Api无法处理此类请求。我使用了querystrings并且它工作了,所以现在我将通过创建一个Signature对象并插入信息并传递对象来传递给api。
此链接可以帮助解决此问题的其他人