0

I wanted to invoke a Simple WCF Service that is hosted in my local using SP.WebRequestInfo class in my SharePoint Hosted app. I was able to get it working without an parameter but as soon as I add set_body I get an error. 400-Bad Request. This call works super good in plain jQuery. I am guessing it is because of body format I am passing. I had tried all sort of formats to pass the value no luck. Please help.

function getVal() 
{
    "use strict";
    var context = SP.ClientContext.get_current();
    var request = new SP.WebRequestInfo();
    request.set_url(        "http://oops/EatAndSplit.svc/Split2"        );
    request.set_method("POST");
    // We need the response formatted as JSON.
    request.set_headers({ "Accept": "application/json; charset=utf-8" });
    request.set_body(JSON.stringify({ data: { "x": "1" } }));
    response = SP.WebProxy.invoke(context, request);
    context.executeQueryAsync(successHandler, errorHandler);
}

2 Answers 2

1

400 Bad Request means that something in your request can not be understood/serialized by the server. If it is only occurs, when you add pageload/body to your request, then your it is malformed or doesn't contain the right arguments.

I'd recommend to to try this code directly in web console and see if you get the same error, even try this code rewritten with jQuery.ajax or SP.RequestExecutor, just to exclude possible problems related with SP.WebRequestInfo

When I look at your code, I'd say you don't need "data", just:

request.set_body(JSON.stringify( { "x": "1" } ));

But it can be anything. See the WCF service, what arguments the service method is expecting.

0

You are probably having cross domain access issue. Implement CROS on your web service project, there are couple readily available in nugget if you need help.

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.