webapi做为后端接口时在跨域调用时的注意点
2017-05-30 15:29
127 查看
比如一个典型的前端跨域调用:
$.ajax({
url: url,
data: params,
dataType: ‘jsonp’,
jsonpCallback:’jsonpcall’,
contentType: “application/json; charset=utf-8”,
type: ‘get’,
success: function (msg) {
mask.remove();
showMsg(msg + ‘success’);
if (typeof callback_s === ‘function’) {
callback_s(msg);
}
},
error: function (xhr, status, error)
{ console.log(xhr); }
});
其中jsonpCallback要求接口在返回数据时调用jsonpcall 函数,所以后台接口在返回数据时使用:
public HttpResponseMessage getFuwuShanagAll()
{
var newdata= new
{
data = fuwu.SelectFuwuShanagAll()
};
string output = JsonConvert.SerializeObject(newdata);
output = “jsonpcall(” + output + “)”;
return new HttpResponseMessage { Content = new StringContent(output, System.Text.Encoding.UTF8, “application/json”) };
}
其中对要返回的数据使用了jsonpcall()函数包裹。
要返回的数据使用了
的序列化。
$.ajax({
url: url,
data: params,
dataType: ‘jsonp’,
jsonpCallback:’jsonpcall’,
contentType: “application/json; charset=utf-8”,
type: ‘get’,
success: function (msg) {
mask.remove();
showMsg(msg + ‘success’);
if (typeof callback_s === ‘function’) {
callback_s(msg);
}
},
error: function (xhr, status, error)
{ console.log(xhr); }
});
其中jsonpCallback要求接口在返回数据时调用jsonpcall 函数,所以后台接口在返回数据时使用:
public HttpResponseMessage getFuwuShanagAll()
{
var newdata= new
{
data = fuwu.SelectFuwuShanagAll()
};
string output = JsonConvert.SerializeObject(newdata);
output = “jsonpcall(” + output + “)”;
return new HttpResponseMessage { Content = new StringContent(output, System.Text.Encoding.UTF8, “application/json”) };
}
其中对要返回的数据使用了jsonpcall()函数包裹。
要返回的数据使用了
Anonymous
and Weakly-Typed Objects
的序列化。
相关文章推荐
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- 设置接口跨域调用方法
- angularjs前后端分离调用接口传递中文时中文乱码解决
- PHP中如何防止跨域调用接口
- jQuery实现跨域iframe接口方法调用
- 跨域调用web.py的restful接口报:OPTIONS https://... 405 (Method Not Allowed)
- 关于AJAX调用ASP.NET MVC 或者WEBAPI项目提示I跨域问题的解决办法
- 关于微信网页调用js-sdk相关接口注意事项目(一级域名与二级域名互相干扰!!!)
- 利用JS-SDK微信分享接口调用(后端.NET)
- web调用通过ocx接口传rtsp流注意事项
- 在小程序后端中转获取接口数据,绕过前端调用限制
- 【知识笔记】前端JS、逻辑层接口、后端SQL之间的调用关系
- WebAPI——web端跨域调用
- 前后端分离,如何防止接口被其他人调用或恶意重发
- webapi使用System.Web.Http.Cors配置跨域访问的几点注意事项
- 跨域调用接口的方法之一:$.ajaxSetup()
- 百度、雅虎、谷歌搜索引擎接口调用注意事项
- 关于AJAX跨域调用ASP.NET MVC或者WebAPI服务的问题及解决方案
- 小程序调用后端接口服务 配置文件详解
- (webservice,ajax,jmail)Tip:动态调用webservice,ajaxpro的使用要点以及使用jmail接口使用注意事项