Hello guys I have a problem where i'm not able to Get JSON Value from client PC.I have created WCF REST service and add the service inside IIS on Client PC.From client PC,open my website and this method readIndicator triggered.But received an error message.I added Access-Control-Allow-Origin at web.config already.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/weightservice/WeightService.svc/GetReader. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
WCF web.Config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="BandRate" value="2400"/>
<add key="Parify" value="0"/>
<add key="Port" value="COM3"/>
<add key="Bits" value="8"/>
<add key="Model" value="BDI2001B"/>
<add key="DecimalPoint" value="0"/>
<add key="Output" value="BDI-2001"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<authorization>
<allow users="*"/>
</authorization>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WCFWeightService.WeightService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="WCFWeightService.IWeightService" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
My Website
function readIndicator() {
try {
$.ajax({
type: "GET",
dataType: 'json',
async: true,
contentType: "application/x-www-form-urlencoded",
url: 'http://localhost/weightservice/WeightService.svc/GetReader',
success: function (data) {
if (data != "" || data != null) {
console.log(data);
console.log(data.GetReaderResult);
$("#indicator").text(data.GetReaderResult);
}
},
error: function (request, status, error) {
//gmyMsgbox('Failed to perform CheckToyNameExist', 'Err');
//err.preventDefault();
console.log(error);
},
});
} catch (err) {
//gmyMsgbox(err.message, 'Err');
console.log(err);
err.preventDefault();
}
}
Client PC Windows XP 32 bit
My PC Windows 10
I have tried with my own PC, no problem.I don't know what mistake that I have make.H ope you guys can help me solve this problem Thank you in advance.
现在,我刚刚在客户端PC浏览器上安装了“允许CORS:Access-Control-Allow-Origin”插件。
I have a solution solving the wcf service cross-domain problem.
Add the global.asax file to project and add the following code to the Begin_request Event.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Don't hesitate to let me know if the problem still exists.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.