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>
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?
– Douglas Santos
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.
– Douglas Santos
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.
– J. Bruni
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.
– Douglas Santos
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/
– J. Bruni
It’s really a connection problem...
– J. Bruni
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).
– J. Bruni
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.
– Douglas Santos
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.
– J. Bruni
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.
– Douglas Santos
Nothing done. I remain stuck at the same point, I have been advised to modify to
$http.get
in place ofjQuery.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.– Douglas Santos
Tip 1: Remove the line
headers: { "Accept": "application/json;odata=verbose" },
– J. Bruni
Tip 2: include
alert(_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('ProductList')/items?$select=ProductName,ProductRate,ProductImage");
beforejQuery.ajax({
- make sure the address is right - copy and paste here for us what appears in Alert.– J. Bruni
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.
– J. Bruni
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'.
– Douglas Santos
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;
– J. Bruni
I changed it as you suggested and it worked as it should. Man thank you very much.
– Douglas Santos
let’s go continue this discussão in chat
– Douglas Santos