Angularjs + Sharepoint + Rest API

Asked

Viewed 758 times

4

People I am currently trying to develop a script within Sharepoint using the Script Editor but it is falling into the error of my code, as if the connection was not correct, however, in google’s Http Dev the Rest request works. Follows the code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<div ng-app>
    <b>Welcome to AngularJS world in SharePoint 2013!</b>
    <div ng-controller="MyController" class="ng-scope">
        <div ng-repeat="p in Products">
            <table style="background-color:#f07432">
                <tr><td align="center"><b>Product Name: {{p.ProductName}}</b> </td></tr>
                <tr><td align="center"><img ng-src={{p.ProductImage}} /> </td></tr>
                <tr><td align="center"><b> Rate: Rs. {{p.ProductRate}}.</b></td></tr>
            </table>
            <hr />
        </div>
    </div>
</div>
<script type="text/javascript">
    function MyController($scope) {
        $scope.loadREST = function () {
            jQuery.ajax({
                url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('ProductList')/items?$select=ProductName,ProductRate,ProductImage",
                type: "GET",
                headers: { "Accept": "application/json;odata=verbose" },
                success: function (data) {
                    var newData = [];
                    jQuery.each(data.d.results, function (index, value) {
                        var pImage = value.ProductImage;
                        prImage = pImage.substring(0, pImage.indexOf(','));
                        newData.push({ ProductName: value.ProductName, ProductRate: value.ProductRate, ProductImage: prImage });
                    });
                    $scope.$apply(function () {
                        $scope.Products = newData;
                    });
                },
                error: function () {
                    alert("error");
                }
            });
        };
        $scope.loadREST();
    }
</script>

2 answers

1


If you are entering the error condition, this means there has been some problem with the HTTP request made via Ajax.

In the code shown, the variable _spPageContextInfo is not defined, but is used:

url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('ProductList')/items?$select=ProductName,ProductRate,ProductImage",

Make sure that the URL effectively contains the correct address.

I recommend using either Firebug in Firefox or Developer Tools in Chrome.

With Chrome, it’s easy to analyze every detail of an HTTP request. Press SHIFT+CTRL+I, click on the tab "Network", reload your page and watch the results - your HTTP call made via Ajax by jQuery should appear. Then, right-click on the HTTP call that appeared in the list, then click "Copy All as HAR" - and then paste the result here into the question as it will contain all the details of the HTTP call that is failing.

  • J. Bruni didn’t work out the way you said. As it goes directly into my error condition (which is shown on the screen whenever I reload) the problem is on the connection, as it does not do what is inside Success. I don’t know yet what it might be. Some other idea?

  • I am running straight through Sharepoint within a Script Editor where it has to work. The request has already tested including putting the same address that I use in Chrome’s Http Dev where it works and brings the items correctly. The error when running on ie10 direct from Sharepoint using ie10’s own console is: SCRIPT7002: Xmlhttprequest: Network Error 0x2ee7, Could not complete operation. Error: 00002ee7.

  • I looked up the 0x2ee7 error means "The server name could not be resolved" - that is, either the address in the URL is really wrong, or there is some DNS problem.

  • But why does the same address work in Http Dev? I did what you suggested to open the Chrome log but a lot of things came up, I don’t know which copy.

  • I don’t know - maybe he removes the whitespace automatically - I’m suggesting this because I just read it here: http://www.pc-library.com/errors/error-code/12007-0x2E7/

  • It’s really a connection problem...

  • Hold on... actually, there are many other factors for an HTTP request to be successful - the headers (headers), cookies, etc, can be as important as the address (URL).

  • The problem is that I am new using this and I don’t know if there is something else wrong with the code. The header I am putting the same as in Dev HTTP.

  • This error is definitely not about the code, but about the HTTP request itself. You may need to tweak the code to include a header, change the URL, include a cookie. The ideal would be to have an exact X-ray to compare the two HTTP requests. On Linux, I would use Wireshark - I have no idea of a similar "network sniffing" tool for Windows.

  • And I’m gonna look for something like that so I can get a better look and put it here as soon as I get any more leads.

  • Nothing done. I remain stuck at the same point, I have been advised to modify to $http.get in place of jQuery.ajax() but I don’t really know how to do it and I guess that didn’t work either. Any more ideas? I’ve exhausted mine.

  • Tip 1: Remove the line headers: { "Accept": "application/json;odata=verbose" },

  • Tip 2: include alert(_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('ProductList')/items?$select=ProductName,ProductRate,ProductImage"); before jQuery.ajax({ - make sure the address is right - copy and paste here for us what appears in Alert.

  • Tip 3: Using Devhttp, save the successful request, then go down, under "REPOSITORY" - "Export", select the request to save it to a file json - and then make this file available to us (either by editing the question and pasting the content, or by using a service such as Pastebin or other) - there we can have more clues to help.

  • When I went to put the full address I noticed that I was putting as http and dev was as https. Apparently it was because he entered my Success where I put an Alert to confirm if it had entered, but another error arose. Alert works but is experiencing this error: SCRIPT438: Object does not support 'index' property or method'.

  • That’s... that’s what you said from the beginning: "Make sure that the URL actually contains the correct address." -) As for the error in "indexof", it means that the variable "pImage" is not string. To force it to be string, Express change the previous line to var pImage = "" + value.ProductImage;

  • I changed it as you suggested and it worked as it should. Man thank you very much.

Show 13 more comments

0

Tries to change the:

url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('....

for:

url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/getByTitle('....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.