locked
Ajax call to WCF Web service in Office 2013 App - Not found RRS feed

  • Question

  • Office 2013 App development in VS 2012, Windows 8

    I created OfficeApp using VS 2012 template (creates OfficeApp and OfficeAppWeb directories).

    I created a WcfService.svc in OfficeAppWeb directory.

    Now, What should be the path of the URL for the ajax call to WcfService file?

    • '/WcfService/Add' - gives 'not found' error
    • if I give the complete url (starting with localhost) it will raise an exception (due to cross site scripting).

    It is not clear to me as to what is the root of the OfficeWebApp.

    Note:

    I created WCF Service to add 2 numbers and display the sum (copied example from http://code.msdn.microsoft.com/office/How-to-consume-WCF-service-48b43a79)

    The directory structure used by the sample is different from the VS 2012 created template. This sample works flawlessly but I would like to stick to VS 2012 created directory structure.

    I am not able to figure out what is causing the 'Not found' for WcfService. Also I am not able to set breakpoints in the service implementation.

    Any help appreciated since this has taken up an enormous amount of time already.

     - Jayawanth

    Tuesday, October 22, 2013 12:31 AM

All replies

  • Michael Zlatkovsky actually has a blog post that I think covers exactly what you're looking for, step by step. Check it out here:

    http://blogs.msdn.com/b/officeapps/archive/2013/06/10/create-a-web-service-for-an-app-for-office-using-the-asp-net-web-api.aspx

    Let me know if that's not what you're trying to do.

    Wednesday, October 23, 2013 8:34 PM
  • It addresses my query to a certain extent. I understand better the routing and page svc path addressing for a web svc built with asp/mvc.

    In my specific case, I am using a WCF service on the server side. The sample app I quoted http://code.msdn.microsoft.com/office/How-to-consume-WCF-service-48b43a79 has a different directory structure and also seems to use http: instead of the default https: (produced by VS 2012 Office App template).

    The sample app's WcfService has url relative to root of application:

    $.ajax({  type: 'post',  url: '/WCFService.svc/Add', ...

    For my OfficeApp I have tried "/WCFService/Add" and url '../../WCFService.svc/Add' relative to the Home.html page

    In both cases I get Not found. I am wondering if the problem is due to https being the default in VS 2012 unlike in the sample app. I do not see any obvious place where I can change this in my Office App project in VS.

    - Jayawanth

     

    Wednesday, October 23, 2013 11:27 PM
  • Did you find an answer to this? I have exactly the same problem.
    Tuesday, November 12, 2013 1:07 PM
  • No I did not.

    -        Jayawanth

    Tuesday, November 12, 2013 5:31 PM
  • I even tried to deploy the example from microsoft to my server and got exactly the same error again. Is it a office security problem?
    Wednesday, November 13, 2013 8:37 AM
  • Hi You need to implement cross domain resource sharing and do ajax calls with full url. You can implement cross domain with jsonp or use json with cors enabled server (on request type OPTION respond with headers required for cors.

    Anze Javornik

    Wednesday, November 13, 2013 10:28 PM
  • Not that, this sample http://code.msdn.microsoft.com/office/How-to-consume-WCF-service-48b43a79 does not use cross domain resource sharing. It is using http however and a different directory structure from the typical one VS 2012 creates.

     - Jayawanth

    Thursday, November 14, 2013 7:17 PM
  • With developer tool (F12) go to networking tab and click start capturing. Then find the request for your ajax call and check to which url it was made (mouse over to so compelte url instead of relative path) and check for incosistencies between the url and your directory structure.

    Anze Javornik

    Thursday, November 14, 2013 9:08 PM
  • Is this a cross domain problem?

    I use 

    $.ajax({
            type: 'post',
            url: 'https://subdomain.domain.com/SaveDocService.svc/SaveAllContent',

    The SaveDocService service is added in the Office app project and the web application is deployed to the same server ajax try to call: https://subdomain.domain.com

    I get "not found".

    If a try to change https://subdomain.domain.com to http://servername/Web app name/SaveDocService.svc/SaveAllContent I get access denied.

    So the service and the office app are on the same server and webapplication

    Friday, November 15, 2013 11:36 AM
  • If in javascript document.domain (or window.location.host) is not equal to the domain of ajax request, then you ajax is making cross domain request. Every subdomain is different: so server.com is different then something.server.com

    Anze Javornik

    Friday, November 15, 2013 12:56 PM
  • Ok than this is not a cross domain problem.

    Ajax request url is exactly the same as the domain where the web application resides.

    This works when debugging.

    Friday, November 15, 2013 1:41 PM
  • I would try to alert window.location.host and document.domain when not debugging. It might be that office app is actually runing localy.

    Anze Javornik

    Friday, November 15, 2013 1:44 PM
  • When alerting both window.location.host and document.domain I get the same url as the url of the ajax request: Subdomain.domain.com
    Friday, November 15, 2013 2:18 PM
  • When you said "If a try to change https://subdomain.domain.com to http://servername/Web app name/SaveDocService.svc/SaveAllContent I get access denied." What if you try httpS://servername/... What kind of error is then?

    Anze Javornik

    Friday, November 15, 2013 4:46 PM
  • I get access denied there also.

    Ajax request url with result:    

    • http://servername/Web app name/SaveDocService.svc/SaveAllContent  - Access denied
    • https://subdomain.domain.com/SaveDocService.svc/SaveAllContent  -  Not found
    • https://servername/Web app name/SaveDocService.svc/SaveAllContent  - Access denied

    Alert in javascript:

    •  window.location.host  - Result: subdomain.domain.com
    •  document.domain - Result: subdomain.domain.com


    • Edited by Toniman2 Friday, November 15, 2013 8:40 PM
    Friday, November 15, 2013 8:38 PM
  • Do you have ssl binding for subdomain.domain.com set up on IIS? If you go to wcf https address with browser do you see your service?

    Anze Javornik

    Friday, November 15, 2013 10:12 PM
  • Yes and yes, but I get the message "Metadata publishing for this service is currently disabled" on top of page

    Friday, November 15, 2013 10:21 PM
  • Access denied is easy to fix. Not found error is about tracing... If you open your app in browser, does itwork?

    Anze Javornik


    Friday, November 15, 2013 10:54 PM
  • When I open the app in browser it will show the aspx/html file but of course will not have dynamic code since its a office app. all functions other than the service call works in the app inside word. When I  do service call within the office app I get "not found".
    • Edited by Toniman2 Friday, November 15, 2013 11:09 PM
    Friday, November 15, 2013 11:08 PM
  • You could try to maku a button with which you preform tha ajax call, which you can test in browser. If you were to fix access denied problem, would that work for you? Or you need to fix not found error?

    Anze Javornik

    Friday, November 15, 2013 11:14 PM
  • Its ok for me to fix the access denied error,but this is a solution that I want customers to deploy to their server also. It has to work there to.
    Saturday, November 16, 2013 9:11 AM
  • For access denied you can implement cors on server side by replying with headers required ( Access-Control-Allow-Origin: * ) to request type OPTIONS . This will make cors work on cors enabled browsers (IE10, Firefox, Chrome, ...) . To support IE9 and IE8 and other not cors enabled browsers you need to use dataType: jsonp in ajax call. This also requires server side support but it only supports GET requests. For not found error try to use fiddler and see where the request is actally beeing sent.

    Anze Javornik

    Saturday, November 16, 2013 10:01 AM
  • Hi again!

    What I need is to upload document from an office app 2013 to sharepoint 2013 server.

    I can´t tell customers to fix cross domain settings on browser. 

    Is it possible to fetch the current document to code behind is server code.

    I use  Office.context.document.getFileAsync to get the document in javascript.

    I can then use the sharepoint client object model to upload the document.

    How is the best way to do this?

    Toni



    • Edited by Toniman2 Friday, November 29, 2013 11:03 AM
    Friday, November 29, 2013 10:59 AM