No ‘Access-Control-Allow-Origin’ header is present on the requested resource详解编程语言
2023-06-13 09:20:31 时间
No Access-Control-Allow-Origin header is present on the requested resource. Origin http://test.123.com:808 is therefore not allowed access.
出现这种问题是时因为浏览器出现了跨域的请求。在http://test.123.com:808网站中请求了其他域中的页面。
有两种方法进行修改。
1使用jsonp的方法修改
使用jsonp的方法其实就是类似于请求js脚本的方法,js脚本的是允许跨域的。
jquery的写法如下:
$.ajax({ type: "post", dataType: "jsonp", url: "http://test.123.456.cn/wxkq/Portlets/ToDoInfo.ashx?OperationType=todocount", data: data, error:function (or) { success: function (or) { });
核心的是dataType修改为jsonp,但实际过程和一般的json差别特别大。
因此后台也需要进行一定的修改
public void ProcessRequest(HttpContext context) context.Response.ContentType = "application/json"; OperationResults or = new OperationResults(); string uid = GetFormString("userid"); string Groupid = GetFormString("Groupid"); string Callback = GetFormString("Callback"); if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(Groupid)) else int userid = int.Parse(GetFormString("userid")); UserSession se = initUserSession(userid, Groupid); ToDoCountAction(or, se); string returnStr = NewtonsoftConver.ObjToJsonString(or); context.Response.Write(Callback + "(" + returnStr + ")"); }
核心是会添加一个查询字符串Callback,然后返回Callback( 返回结果 ),到了前台其实就是一个js脚本,就相当于在前台调用了一个js方法
Callback( 返回结果 ),只不过这个js函数的传入参数是后台生成的。
2后台方法允许跨域
public void ProcessRequest(HttpContext context) context.Response.AddHeader("Access-Control-Allow-Origin", "http://test.123.com:808"); context.Response.ContentType = "application/json"; string Callback = GetFormString("Callback"); string returnStr = NewtonsoftConver.ObjToJsonString(or); context.Response.Write(Callback + "(" + returnStr + ")"); }
核心
context.Response.AddHeader( Access-Control-Allow-Origin , http://test.123.com:808 );
允许任意网站访问
ntext.Response.AddHeader( Access-Control-Allow-Origin , * );
对于HTML页面可以添加,但没有测试
meta http-equiv="Access-Control-Allow-Origin" content="*"
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11223.html
cjava相关文章
- ORA-26695: error on call to string: return code string ORACLE 报错 故障修复 远程处理
- ORA-26735: operation not allowed on the specified file group version ORACLE 报错 故障修复 远程处理
- ORA-32401: materialized view log on “string”.”string” does not have new values ORACLE 报错 故障修复 远程处理
- ORA-48165: user missing read, write, or exec permission on specified ADR Base directory [string] ORACLE 报错 故障修复 远程处理
- ORA-55619: Invalid privilege to grant on Flashback Archive ORACLE 报错 故障修复 远程处理
- ORA-10618: Operation not allowed on this segment ORACLE 报错 故障修复 远程处理
- ORA-16150: FINISH recovery performed on another, older standby database ORACLE 报错 故障修复 远程处理
- [email protected]_on条件编译详解编程语言
- VASP on Linux:Unlock the Potential of HighPerformance Computing(vasplinux)
- Oracle 等待事件 cursor: pin S wait on X 官方解释,作用,如何使用及优化方法
- jquery关于on click事件的理解详解编程语言
- 在PROCESS ON VALUE-REQUEST事件中获取屏幕值详解编程语言
- Boost Your File Compression Efficiency with 7z on Linux(7zlinux)
- MySQL中如何使用ON语句(mysql中 on怎么用)
- 解析sql语句中left_join、inner_join中的on与where的区别