显然,我完全误解了它的语义.我想到了这样的事情:
好吧,我错了.它根本不起作用.所以,我已经阅读了跨源资源共享,并尝试在w3c推荐中阅读跨源资源共享
有一件事是肯定的 - 我仍然不明白我应该如何使用这个标题.
我完全控制了站点A和站点B.如何启用从站点A下载的javascript代码以使用此标头访问站点B上的资源?
PS
我不想使用JSONP.
我正在尝试允许来自我在localhost:80托管的javascript应用程序的POST请求到托管在不同端口的WCF REStful服务,但不知何故它不起作用.我已经尝试在标题中添加自定义属性,并在我的服务JSONData方法中以编程方式添加它,但我仍然在我的响应中得到'405 Method not allowed'.这里适当的方法是什么?
这是我的界面:
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string JSONData()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
return "Your POST request";
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
和服务代码:
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Script.Services;
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[ScriptMethod]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "export")]
string JSONData();
}
}
Run Code Online (Sandbox Code Playgroud)
最后配置:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service …Run Code Online (Sandbox Code Playgroud) 我需要使用JavaScript加载和读取XML文件.
以下代码在Firefox,IE和Opera中运行良好:
function loadXMLDoc(dname) {
var xmlDoc
// Internet Explorer
try {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM')
}
catch (e) {
// Firefox, Opera, etc.
try {
xmlDoc = document.implementation.createDocument('', '', null)
}
catch (e) {
alert(e.message)
}
}
try {
xmlDoc.async = false
xmlDoc.load(dname)
return xmlDoc
}
catch (e) {
alert(e.message)
}
return null
}
Run Code Online (Sandbox Code Playgroud)
但是在Chrome中执行此代码会给我带来以下错误:
对象#没有方法"加载"
cross-domain ×2
javascript ×2
asp.net ×1
c# ×1
cors ×1
http-headers ×1
wcf ×1
xml ×1
xmldom ×1