0

I get a 400 Bad Request from this below code, what can the problem be?

'use strict';
var hostweburl;
var appweburl;

$(document).ready(function () {
    hostweburl =
      decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl =
       decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var scriptbase = hostweburl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.RequestExecutor.js");
});



function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }

}


function addItem() {
    var executor = new SP.RequestExecutor(appweburl);
    var rest_data = JSON.stringify({ 
'__metadata': { 'type': 'SP.Data.FriendsListItem' }, 
 'Title': $("#txtTitle").val(),  
'Firstname': $("#txtFirstName").val(), 
'Lastname': $("#txtLastName").val()
 });

    executor.executeAsync(
                 {
                     url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('studentdetails')/items/?@target='" +
            hostweburl + "'",
                     method: "POST",
                     data: rest_data,
                 headers: { "content-type": "application/json; odata=verbose" },
                  success: successHandler(),
                    error: errorHandler() ,

                 }
             );
}

function successHandler() {
    $("#message").addClass("SuccessMessageDiv");
    $("#message").html("Item Added");
}

function errorHandler() {
    $("#message").addClass("ErrorMessageDiv");
    $("#message").html("There was error");
}

1 Answer 1

0

I tried to add a comment, but due to my reputation I am unable to do so.

It would help if you gave us a bit more context about the issue, namely what the script you included above actually does.

My first guess would be that your MaxUrlLength is being exceeded with the Async REST services call. The following line may render a URL with a character count exceeding 260, which is the SharePoint 2013 default.

appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('studentdetails')/items/?@target='" +
        hostweburl + "'",

The length of the URL, in number of characters. The default is 260.

Reference to Default MaxUrlLength

1
  • Hi Clinton Person, I changed MaxUrlLength value in corresponding web.config file but same error displaying.
    – jegan
    Commented Jun 23, 2015 at 5:37

Your Answer

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

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