Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 12, 2011 @ 23:42
    Bo Damgaard Mortensen
    0

    Allow cross domain calls for a base method

    Hi all,

    I'm currently working on a project where remote domains needs to retrieve information of a member based on a member id.

    My initial thought was to make use of /base for this, but I'm running my head against the wall because of cross domain policy. Is it possible to, sort of, "grant access" to a base method, somehow? The scenario is as follows:

    Domain A (http://www.mydomain.com) makes a call to the Umbraco site (www.umbracodomain.com) /base method which takes a member ID as a parameter. The data returned will be XML containing the info about this member.

    I have a weird feeling that I've overlooked something here :-) Any help and/or hint on this is greatly appreciated!

    Thanks!

    - Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 12, 2011 @ 23:44
    Bo Damgaard Mortensen
    0

    Should also note that the call from Domain A have to be from JavaScript (and no jQuery!)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 13, 2011 @ 11:42
    Bo Damgaard Mortensen
    0

    Nevermind this. I went and used JSONP to simulate a cross domain request. So basically what I did was:

    In javascript on the external domain:

    function callback(data) {
    alert("Member ID: " + data.id + "Member name: " + data.name);


    function jsonpCall() {
    var script = "<script type='text/javascript' " +
    "src='http://myumbracodomain.com/" +
    "base/basemethod/GetMemberById/1052.aspx?callback=callback'><\/script>";
    $("#inject").html(script);

    And my base method:

    public static string GetMemberById(string memberId)
    {
    Member m = new Member(Int32.Parse(memberId));
    return "callback( {\"id\": \"" + m.Id + "\", \"name\": \"" + m.Text + "\"} );"; 

     

  • Peter Cort Larsen 421 posts 1038 karma points
    Apr 23, 2012 @ 10:59
    Peter Cort Larsen
    0

    Hi,

     

    Do you have a full example i could see ?

  • Peter Cort Larsen 421 posts 1038 karma points
    Apr 23, 2012 @ 11:29
    Peter Cort Larsen
    0

    I can call a base method like 'GetMemberById' and get a response, but when i use your javascript, i get no response.
    So if you have some working demo code, i would really like to see it.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Apr 23, 2012 @ 20:34
    Bo Damgaard Mortensen
    0

    Hi Peter,

    Thanks for diggin' up this old post :-) I'll have to look through my codesnippets when I get home later tonight. Will bookmark this on my phone so I won't forget it ;-)

  • Peter Cort Larsen 421 posts 1038 karma points
    Apr 23, 2012 @ 20:42
    Peter Cort Larsen
    0

    Thanks Bo, i hope you find it.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Apr 24, 2012 @ 09:25
    Bo Damgaard Mortensen
    0

    Hi Peter,

    Unfortunately my code for this is scattered to all directions since I got a new hdd and started using DropBox for storing all my codesnippets :-(

    I'm curious though, what happens if you make a regular $.ajax call to your base method? Something along the lines of this:

    $.ajax({
    url: 'http://myumbracodomain.com/base/basemethod/GetMemberById/1052.aspx?callback=?',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
    alert(data);

    }); 

    The base method shouldn't really be caring about the callback parameter in this case.

    Let me know if you get anything back by doing this :-)

  • Peter Cort Larsen 421 posts 1038 karma points
    Apr 24, 2012 @ 14:32
    Peter Cort Larsen
    0

    It dosnt return anything, neither from remote web app or from same web app as where the /base call is placed.

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Apr 24, 2012 @ 16:39
    Bo Damgaard Mortensen
    0

    Hi Peter,

    Alright, so the first step will have to be making the /base method work :-) Can I trick you into posting your code for the base method and the snippet in your restExtensions.config file here? Also, what does it tell you when trying to access the base method by typing it directly in the browser? Sometimes this approach makes it easier to track down errors like nullpointer exceptions and studd like that.

    Thanks again.

    Bo

     

  • Peter Cort Larsen 421 posts 1038 karma points
    Apr 24, 2012 @ 21:38
    Peter Cort Larsen
    0

    Base call:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.presentation.umbracobase;
    using umbraco.cms.businesslogic.member;

    namespace Ontranet.Test.Code
    {
        [RestExtension("test")]
        public class test
        {
            [RestExtensionMethod()]
            public static string IsAuthenticated(string MemberName) {

                Member m = Member.GetMemberFromLoginName(MemberName);

                if (m.getProperty("isAuthenticated").Value.ToString() == "1")
                {
                    return "true";
                }
                else
                {
                    return "false";
                }
        
            }
            [RestExtensionMethod()]
            public static string GetMemberById(string memberId) {
                Member m = new Member(Int32.Parse(memberId));
                return "callback( {\"id\": \"" + m.Id + "\", \"name\": \"" + m.Text + "\"} );";
            }
        }
    }

    rest file:
    <ext assembly="Ontranet.Test.Code" type="Ontranet.Test.Code.test" alias="test">    
        <permission method="IsAuthenticated" allowAll="true" returnXml="true" />
        <permission method="GetMemberById" allowAll="true" returnXml="true" />
      </ext>

    A call directly to http://umb_4.7.1.1.com//base/test/GetMemberById/1195.aspx returns:

    <value>callback( {"id": "1195", "name": "CustomerName entered in member section"} );</value>

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Apr 26, 2012 @ 23:45
    Bo Damgaard Mortensen
    0

    Hi Peter,

    sorry for the late response, it's been some rough days lately.

    Have you tried to tell the base config to not return xml? returnXml="false"

    Also, what happens if you remove the ?callback=? parameter from the ajax call?

    Does it return anything if you pass

    http://myumbracodomain.com/base/basemethod/GetMemberById/1052.aspx?callback=?

    Into the browser? :-)

    I can't make a testsite for this at the moment, so that's why I'm asking all these questions ;-)

Please Sign in or register to post replies

Write your reply to:

Draft