Copied to clipboard

Flag this post as spam?

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


  • Frederik T 234 posts 345 karma points
    Feb 12, 2013 @ 12:46
    Frederik T
    0

    Contacting web services API from a razor script

    Ok so this is making me and my collegues scratching our heads.

    We have developed our own internal web services api to contact our databases, and thought it would be straightforward to use it with razor, and combine it with knockout js. Well, after a ton of trail and error, it still doesnt work, and we have somehow nailed it down to umbraco blocking the web service.

    I have searched around and either solutions we found doesnt work, or the information is inconsistent.

    This is our razor script:


    <script src="/Scripts/knockout-2.1.0.js"></script>
    <h1>Tilmed </h1>
    <p>Name: <strong data-bind="text: name"></strong></p>
    <p>Email: <strong data-bind="text: email"></strong></p>

    <p>Name: <input data-bind="value: name" /></p>
    <p>Email: <input data-bind="value: email" /></p>

    <script type="text/javascript">

        var ViewModel = function() { //Make the self as 'this' reference
            var self = this; //Declare observable which will be bind with UI
            self.name = ko.observable("enter name");
            self.email = ko.observable("enter email");

            getMPerson();

            function getMPerson() {

     $.ajax({
            // the url to the service
             url: "http://webapi.OURSITE.dk/api/MPersons/123456",
            // the format that the data should be in when
            // it is returned
             contentType: "json; charset=utf-8",
            // the function that executes when the server
            // responds to this ajax request successfully
            success: function (data) {
                // Update view model properties
                self.name(data.Name);
                self.email(data.Email);
            },
            error: function (error) {
                alert("ERROR STATUS: " + error.status + "  STATUS TEXT: " + error.statusText);
            }
        });
        }

        }; // end viewmodel definition
        ko.applyBindings(new ViewModel());
      </script>

    Any suggestions? Some oversights or anything?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Feb 14, 2013 @ 00:54
    Bo Damgaard Mortensen
    0

    Hi Frederik,

    What kind of service have you created? .asmx? svc? /base? Also, if you hit F12 in Google Chrome and go to the network tab, do you get any response when invoking your ajax call?

    All the best,

    Bo

Please Sign in or register to post replies

Write your reply to:

Draft