关于C#中ajax跨域访问问题
作者:CDL_Darren 时间:2023-03-04 21:28:04
最近因项目需要,需要跨域请求访问数据。跨域访问是指什么?
[跨域]:指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制。所谓同域是指,域名,协议,端口均相同,不明白没关系,举个栗子:例如,我的电脑上有2个服务器 192.168.0.11和192.168.0.12。如果第一个服务器上的页面要访问第二个服务器上面的数据,就叫做跨域。或者http://www.baidu.com 要访问http://www.xxx.com也是不同域名也是跨域。 下面给出完整请求案例:
前端页面请求代码片:
<script type="text/javascript">
function ajaxsubmit(name,phone) {
$.ajax({
type: "get",
url: "http://10.10.10.132:35709/AppInterface/ResourceInsert.ashx",
data: { "share_name": encodeURI(name), "telphone": encodeURI(phone), "fromtype": 4 },
dataType : "jsonp",
jsonp: "callback",
jsonpCallback: "successcallback",
success: function (json) {
alert(json.msg);
},
error:function(e){
alert("提交失败!请稍后再试");
}
});
}
</script>
一般处理程序代码片:
public class ResourceInsert : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
cms.Model.Resource model = new Model.Resource();
cms.BLL.Resource bll = new BLL.Resource();
//你所需要进行的操作
model.share_name = HttpUtility.UrlDecode(context.Request["share_name"]);
model.ask_telphone = HttpUtility.UrlDecode(context.Request["telphone"]);
model.back_row_one = context.Request["fromtype"];
ConvertHelper ch = new ConvertHelper();
model.share_name = ch.RemoveSpecialChar(model.share_name);
//successcallback为跨域请求回调函数,切记必不可少。获取方式也可以为context.Request["callback"],
//对应前端页面发起请求的jsonp和jsonpCallback格式为:jsonp_value=jsonpCallback_value
if (bll.Exists(model.share_name, model.ask_telphone))
{
Message temp = new Message(1, "我们已收到您的请求额!请勿重复提交!", null);
context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
context.Response.End();
return;
}
else
{
if (bll.Add(model) > 0)
{
Message temp = new Message(1, "提交成功,我们工作人员会尽快回复你!感谢关注!", null);
context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
context.Response.End();
return;
}
else
{
Message temp = new Message(0, "请确认信息填写无误!", null);
context.Response.Write("successcallback" + "(" + JsonConvert.SerializeObject(temp) + ")");
context.Response.End();
return;
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
你以为到这里完了吗?当然没有/斜眼笑。配置文件中当然不能少,web.config文件中的 system.webServer 节点下 增加如下配置:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
以上所述是小编给大家介绍的关于C#中ajax跨域访问问题网站的支持!
来源:http://blog.csdn.net/cdl_darren/article/details/71730052
标签:c,ajax,跨域
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现带阴历显示的日期代码
2023-12-11 02:19:04
java IO流 之 输入流 InputString()的使用
2023-08-22 07:44:31
android实现可自由移动、监听点击事件的悬浮窗
2022-04-12 14:15:31
基于C#实现的端口扫描器实例代码
2022-04-29 20:21:56
Java编程GUI中的事件绑定代码示例
2023-02-04 17:36:01
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
Java Maven依赖传递,可选依赖,排除依赖详解
2022-06-09 14:24:14
![](https://img.aspxhome.com/file/2023/9/66489_0s.png)
深入Android MediaPlayer的使用方法详解
2023-07-21 21:50:47
Java Runtime的使用详解
2021-10-23 06:46:39
![](https://img.aspxhome.com/file/2023/6/78976_0s.png)
Java Arrays工具类用法详解
2023-12-19 13:08:00
![](https://img.aspxhome.com/file/2023/7/61807_0s.png)
IDEA使用SequenceDiagram插件绘制时序图的方法
2023-07-03 11:20:07
![](https://img.aspxhome.com/file/2023/4/86034_0s.png)
java 实现约瑟夫环的实例代码
2022-06-24 16:26:11
Java 中普通代码块,构造代码块,静态代码块区别及代码示例
2022-07-03 03:54:21
Java Web Fragment在项目中使用方法详解
2022-04-11 14:17:38
![](https://img.aspxhome.com/file/2023/5/64775_0s.png)
C#遍历子目录的方法
2022-06-30 11:11:27
C#语言中条件与&&与条件或||的区别
2022-10-06 22:51:55
关于使用Mybatisplus自带的selectById和insert方法时的一些问题
2023-11-01 14:15:42
如何使用JCTools实现Java并发程序
2023-11-21 07:37:39
Java结构型设计模式之桥接模式详细讲解
2023-08-26 16:25:56
![](https://img.aspxhome.com/file/2023/2/78062_0s.png)
Java实现二分查找的变种
2023-11-07 11:26:28
![](https://img.aspxhome.com/file/2023/6/85556_0s.jpg)