MS method of creating list item fails with chrome. How to do the same with SPServices?

Nov 12, 2013 at 7:39 AM

I've used the MS recommended way of creating a list item, as specified here:

I have built this in as a button inside an item display template (a search result item for those unfamiliar), which posts data from the current item to a simple list with two columns.

The relevant bits of code in the display template look like this:
var Tittel = $getItemValue(ctx, "Title");
var StartDato = $getItemValue(ctx, "StartdatoOWSDATE");

function postSurvey()
    var siteUrl = '/kursinnhold';
    var context = new SP.ClientContext(siteUrl);
    var myWeb = context.get_web();
    var myList = myWeb.get_lists().getByTitle('testlist');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var listItem = myList.addItem(itemCreateInfo);
    listItem.set_item('Title' , Tittel.value);
    listItem.set_item('Deltakernavn' , StartDato.value);
    context.executeQueryAsync(Function.createDelegate(this, AddNewItemSuccess) ,Function.createDelegate(this, AddNewItemFail));

    function AddNewItemFail(sender, args)
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

    function AddNewItemSuccess(sender, args)
        alert('item created: '+listItem.get_id());


$( document ).ready(function() {
document.getElementById('test').addEventListener('click', postSurvey, false);
<button id="test">Click here to register</button>
This works well with internet explorer - the new list item is created and contains the values it gets from the item.

However, in Chrome, I keep getting an error message saying
"Request failed. Unexpected response data from server.

My question: How can I achieve the same using SPServices/jQuery (hoping it will work better with other browsers)?

I was not able to figure out if it's possible from the documentation, but that could possibly be because I am not proficient enough with jQuery.
Nov 12, 2013 at 10:41 AM
A quick update.

I seem to have underestimated how difficult this would be.

This is the equivalent using SPServices, and even better - this works across all browsers!
function CreateNewItem() {
var kursDato = $().SPServices.SPConvertDateToISO({
    dateToConvert: StartDato.value,
        operation: "UpdateListItems",
        async: false,
        webURL: "/kursinnhold/",
        batchCmd: "New",
        listName: "{98E7E0B1-E8CE-4A79-8414-5C9C030D7969}",
        valuepairs: [["Title", Tittel.value],["Deltakernavn", kursDato]],
        completefunc: function(xData, Status){
            alert("You have now registered your participation!");
Nov 12, 2013 at 12:41 PM
SPServices is much easier to work with the CSOM, IMO. ;+)