0

I'm extremely new to SharePoint and was recently experimenting with the client side object model. I've used one of MSDNS examples. however, I seem to be getting this error response :

"request failed. Unexpected Response from server.The status code of the response is '0'. The status text of the response is null"

Here is the sample of the code I've used:

    function retrieveListItems() {
        var siteUrl = '/personal/amgu/workspace/music/'; // music is a site within a site.
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Music');

        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'Title\'/>' + 
            '<Value Type=\'Text\'>'+Title+'</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');  // trying to query using the title name which is unique and I've retrieved and stored  it in a variable called title.
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    }

    function onQuerySucceeded(sender, args) {

        var listItemInfo = '';

        var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() +
                '\nTitle: ' + oListItem.get_item('Title') +
                '\Votes: ' + oListItem.get_item('Votes'); // this is another column to which i actually want to make an increment after getting the previous value first
        }

        alert(listItemInfo.toString());
    }

    function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

Please tell me is there something wrong with my CAML query?

Note: 1.)the function is being triggered via a button click, Bound to it via jquery bind.

2.) Also happened to notice this in the Console as soon as i click the button:

NS_ERROR_NOT_AVAILABLE: prompt aborted by user     ....    nsPrompter.js
(this error comes immediately after clicking)

Would really appreciate it if someone could throw some light onto the matter hehe.

3.) removed the CAML query function from the code and then tried . Still gave the same error

4.)BUTTON MARKUP:

I've plugged in the vote up and vote down buttons inside the web part title

     $("#WebPartTitleWPQ3").html(Title + ".mp3 : By " + Artist + "&nbsp&nbsp (" + Votes + " votes) ").css("font-size", "20px").append("<button  id='voteBtnUp' > Vote Up </button>&nbsp<button  id='voteBtnDn' > Vote Down </button>");

and the bound the click handler to them like this

 $('#voteBtnUp').bind("click", function () { voteOnClick(Title, "up"); });
         $('#voteBtnDn').bind("click", function () { voteOnClick(Title, "Dn"); });

voteOnClick si simply calling the retrieveListItems() functions for now. haven't used the arguments for anything

Also, tried using get_web() instead while creating the client context, still not working

7
  • Show us the button markup please
    – MdMazzotti
    Mar 25, 2014 at 10:22
  • Also, the code is filled with bugs... see the answers below, to which I add this other bug: clientContext.load(collListItem); should read clientContext.load(this.collListItem);
    – MdMazzotti
    Mar 25, 2014 at 10:51
  • Hmmm.... yeah your right. I got this from MSDN :|
    – Aman Gupta
    Mar 25, 2014 at 10:59
  • lemme fix all that and check
    – Aman Gupta
    Mar 25, 2014 at 10:59
  • See my answer below
    – MdMazzotti
    Mar 25, 2014 at 11:00

5 Answers 5

2

Change the button markup from

<button  id='voteBtnUp' > Vote Up </button>
<button  id='voteBtnDn' > Vote Down </button>

to

<input type="button" id="voteBtnUp" value="Vote Up"></input>
<input type="button" id="voteBtnDown" value="Vote Down"></input>
5
  • 2
    Thank you so much. Could you please throw some light onto why this happened just simply because I used button markup instead of input?? It's really weird
    – Aman Gupta
    Mar 26, 2014 at 5:35
  • I really wish I could give a sensible explanation to this weirdness. The truth is I stumbled across a blog post the other day, saying something about you should use an input type="button" instead of a button control to fix this error. I doubtfully gave it a try just to realize that it really was it. Don't you love SharePoint?
    – MdMazzotti
    Mar 26, 2014 at 9:31
  • Hahaha!! Well, I'm new at it so it's yet to grow on me! I'm just a humble html5 developer lost in the vast metaphorical sea called Sharepoint :D
    – Aman Gupta
    Mar 28, 2014 at 7:58
  • Weird thing....but working....:D Jun 11, 2015 at 6:15
  • Also note that format given only works <input type="button" id="voteBtnDown" value="Vote Down"></input> i tried to shuffle the attributes and it stopped working.. May 23, 2018 at 13:59
0

If you are using SP 2013, I recommend to take advantage of REST API and make your life easier

var url = _spPageContextInfo.webAbsoluteUrl+ "/_api/Web/Lists/GetByTitle('music')/items?$top=10&$filter=Title Ge '" + Title + "'";
$.ajax({
                url: url,
                type: "GET",
                dataType: "json",
                headers: {
                    Accept: "application/json;odata=verbose"
                }
            }).done(function(data){
var res = data.d.results;
//Do your stuff....
});

Also you can fine tune query to odata select query options http://www.odata.org/documentation/odata-version-2-0/uri-conventions/

3
  • hey! yeah I'd prefer to use REST just one question. How should I go about using it with the fact that my company just migrated to sp2013 server, but the root site collection is still based off sp2010 modules
    – Aman Gupta
    Mar 25, 2014 at 10:53
  • Well you can do stuff on REST only if its SP2013, otherwise you need to migrate your SP2010 site collection. If not create a sample list in 2013 and test
    – stak
    Mar 25, 2014 at 11:20
  • It's SP2010. Unfortunately this option didnt work when i tried. Any other suggestion? :(
    – Aman Gupta
    Mar 26, 2014 at 5:12
0

The culprit is the SharePoint 2013 MDS feature. I am on mobile at the moment so cannot provide any links but you can google about this MDS.

You try to turn it off at site features : minimum download strategy and then let me know if same issue is present.

This feature is supposed to make life easy and it does so when using the ootb solutions. But it creates a mess when using custom js code solutions.

I have answered this question only looking at the strange error message which does not give any constructive info to developers. And I got stuck on such error for whole day before discovering MDS was culprit.

Good luck

Links:

The correct way to execute JavaScript functions in SharePoint 2013 MDS enabled sites

http://www.greggalipeau.com/2013/09/18/sharepoint-2013-dynamic-javascript-and-minimal-downloads-the-_layouts15start-aspx-conundrum/

9
  • Hmmmm.. good point let me try that
    – Aman Gupta
    Mar 26, 2014 at 4:50
  • If at all this is the problem and you happen to find a way to make a javascript code MDS compliant, do update me.
    – variable
    Mar 26, 2014 at 4:52
  • I checked the site features list from site settings. MDS dosent seem to be there. Is this a feature of SP2013? the current site I'm working on is SP2010 based. Also, it's a sub site.
    – Aman Gupta
    Mar 26, 2014 at 4:56
  • I hope it's not confusing when I mention this too that the root site collection is based on 2010 but is being hosted on sp2013 server. hehe
    – Aman Gupta
    Mar 26, 2014 at 4:57
  • My answer is with reference to SP2013. 2013 SP site has this settings under site features
    – variable
    Mar 26, 2014 at 5:04
0

Try changing var listItemEnumerator = this.collListItem.getEnumerator();

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = this.collListItem.getEnumerator();
    ....
}

And your camlQuery looks more or less fine.

2
  • actually, the onQuerySucceeded never gets called back. It's the other one Because i just noticed that the other function is printing the response error. :\
    – Aman Gupta
    Mar 25, 2014 at 10:57
  • Ohh!! I have found the error mentioned often, so I thought here also it is the same. ;)
    – Amit
    Mar 25, 2014 at 11:18
0

Yes Here we can go with button tag but we need to write below lines of code to prevent its default action and we can call our own function on click.

No need to change to input type="bytton" tag

 $("button").click(function (event) {
        event.preventDefault();
    });

put this somewhere in

$(document).ready(function()
{
});

It will work as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.