如何使用webApi在ajax中设置url [英] how to set url in ajax with webApi

查看:227
本文介绍了如何使用webApi在ajax中设置url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Web API进行文件上传。我在jQuery中有这个代码:





 var ajaxRequest = $ .ajax({
type :POST,
url:api / fileupload / uploadfile,
contentType:false,
processData:false,
data:data
});
ajaxRequest.done(函数(responseData,textStatus){
alert(ok);
}



这在全球范围内.asax:



 GlobalConfiguration.Configure(config => 
{
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name:DefaultApi,
routeTemplate:api / {controller} / {action} / {id},
默认值:new {id = System.Web.Http.RouteParameter.Optional}
);
});




$ b控制器中的$ b这种方法:



 public KeyValuePair< bool,string> uploadfile()
{
}







但是表单没有提交。但是当我发表评论时,将url来源显示警告。如何写URL?有什么问题?

解决方案

.ajax({
类型:POST,
url:api / fileupload / uploadfile,
contentType:false,
processData:false,
数据:数据
});
ajaxRequest.done(函数(responseData,textStatus){
alert(ok);
}



这在全球范围内.asax:



 GlobalConfiguration.Configure(config => 
{
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name:DefaultApi,
routeTemplate:api / {controller} / {action} / {id},
默认值:new {id = System.Web.Http.RouteParameter.Optional}
);
});




$ b控制器中的$ b这种方法:



 public KeyValuePair< bool,string> uploadfile()
{
}







但是表单没有提交。但是当我发表评论时,将url来源显示警告。如何写URL?有什么问题?


你需要s从表单中提取数据,你在哪里实际设置数据变量?您希望从表单上传文件,但上传文件并不像将文本数据发送到服务器那样简单。



另外,在服务器端,您需要更改控制器的动作,该动作接受来自ajax脚本的数据。您需要确保它在请求数据集合中查找文件。您的代码不会显示其中任何一个。



jQuery代码非常简单,



 


(document).ready(function(){
//准备好文件


I use Web API for file upload. I have this code in jQuery:


var ajaxRequest = $.ajax({
     type: "POST",
     url: "api/fileupload/uploadfile",
     contentType: false,
     processData: false,
     data: data
     });
    ajaxRequest.done(function (responseData, textStatus) {
    alert("ok");
    }


and this in global.asax:

GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new {id =System.Web.Http.RouteParameter.Optional }
                );
            });



in controller this method:

public KeyValuePair<bool, string> uploadfile()
    {
    }




But the form does not submit.but when i comment line that put url source it show the alert. How do I write the URL? What is wrong?

解决方案

.ajax({ type: "POST", url: "api/fileupload/uploadfile", contentType: false, processData: false, data: data }); ajaxRequest.done(function (responseData, textStatus) { alert("ok"); }


and this in global.asax:

GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new {id =System.Web.Http.RouteParameter.Optional }
                );
            });



in controller this method:

public KeyValuePair<bool, string> uploadfile()
    {
    }




But the form does not submit.but when i comment line that put url source it show the alert. How do I write the URL? What is wrong?


You need to submit the data from the form, where are you actually setting the data variable? You wanted to upload the file from your form, but uploading the files is not as simple as it might seem to send a text-only data to the server.

Plus, on the server side you need to change the controller's action that accepts the data from your ajax script. You need to make sure that it is looking for a file in the request data collection. Your code does not show either one of them.

The jQuery code is as simple as,


(document).ready(function () { // On the document ready


这篇关于如何使用webApi在ajax中设置url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆