Allowing Cross Origin Requests

Hi,


I am using the .NET Viewer GroupDocs.Viewer.WebForms project example and I have a question about enabling Cross Origin Requests for this project.

When I make an ajax request from my web project to the Web service (that is hosted on another VM on the network) that returns the InlineDocumentScript I get an error in the developer tools (No ‘Access-Control-Allow-Origin’ header is present on the requested resource.)

Is there a way to allow these requests by altering a method or config file in this project ?

Hi John,


Thank you for the question. To enable the Viewer CORS support in an ASP.NET application, you need to add some HTTP module and handler descriptions to Web.config. For Classic pipeline mode of IIS you should add the following into system.web/httpModules:


Add the following into system.web/ httpHandlers:

For Integrated pipeline mode of IIS you should add the following into system.webServer/modules:

Add the following into system.webServer/ handlers:

Best regards.

It seems all the additions you suggested are already in the web.config file which I will copy and paste below. This was the file that came with the viewer example. That being said, I am still not able to make cross origin requests. Any additional fixes would be appreciated.


<?xml version="1.0" encoding="UTF-8"?>


<system.web>





</system.web>

<system.webServer>
</system.webServer>

Hi John,


Thank you for the information. In such case you just need to use [WebMethod] in the .asmx as Ajax handlers. Example of such file you can find attached.

Best regards.

Hi,


I am already using these methods to return the scripts. I was able to download WebServiceProviderViewer.asmx.cs and verify they are the same, but I was not able to download the other file, I get an error each time I click on the file name.

Again, the error I receive when trying to request the scripts cross origin is a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

Hi,


Thank you for coming back. In such case I can assume that the issue reason is in your JavaScript which sends the Ajax request. Could you please share it with me.

Also could you please provide such info:

1. Project type from which you send the request.
2. Does the projects hosted on the IIS or VisualStudio server?

Thank you.
$.ajax({
url: "http://w12-dctm71-idx:2222/WebServiceProvidingViewer.asmx/GetInlineDocumentScript",
type: 'POST',
data: {url : url, fileName : fileName },
async: false,
crossDomain: true,
contentType: "application/x-www-form-urlencoded"
}).done(function(data){
$("body").append($.parseHTML(data.documentElement.textContent, document, true));
console.log("Adding doucment script to viewer element ........................");
});

I have augmented GetInlineDocumentScript in the webservice to be :

public string GetInlineDocumentScript(String url, String fileName)
{
string groupdocsViewerScript;

groupdocsViewerScript = Viewer.ClientCode()
.TargetElementSelector("body")
.Url(url, fileName)
.UseHtmlBasedEngine(true)
.ShowPrint(false)
.ShowPaging(false)
.ShowViewerStyleControl(false)
.ShowDownload(false)
.ShowFolderBrowser(false)
.Layout(ViewerMode.OnePageInRow)
.Watermark("")
.EnableRightClickMenu(true)
.ShowThumbnails(true)
.OpenThumbnails(false)
.ShowHeader(false)
.ZoomToFitWidth()
.ToString();
return groupdocsViewerScript;
}


1. I am making the request from a standalone html file using the jQuery included in libs (libs/jquery-1.9.1.min.js)
2. The project is hosted on IIS

Hi again,


Thank you for source code example. Know I was able to reproduce your issue. I have resolved it in such way:
1. Remove “crossDomain: true,” from the Ajax
2. Add such code to the <system.webServer>
3. Rebuild the project and restart it in the IIS

Best regards.


Ok, that seemed to resolve the cross origin issue. However, the content I am trying to stream is located in a database and I am returning a stream to the groupdocs viewer via a java servlet. My question is about the Viewer.Url() method.


Does this method have a max string length and does the url passed in need to be URI encoded ?

I have looked at the documentation, (http://groupdocs.com/docs/display/viewernet/Url+Method) but it does not specify.

The reason I ask is because now my viewer continuously loads and never stops loading. No error is returned, it just keeps going.

Hi John,
Glad to hear that the cross domain issue resolved.
To validate the URL we use only such method. Also the URL should be direct URL to the document, for example http://localhost/My document.pdf
Best regards

Thanks, I have resolved all issues. However is there a way to disable caching ?

Hi,


Glad to hear that the all issue are resolved. Unfortunately there is no way to disable the cache. It required by the Viewer because it creates HTML or Image representation of the document and need to store it somewhere.

Best regards.