0

I've been at this for hours now and still not got it right... someone please put me out of my misery!

javascript:

        var calcParams = {}
        calcParams.calcName="gwp_mat";
        calcParams.components=124.332;

        //return manager.impactCalc(calcName, componentArray)
        return postData("{calcParams2:" + JSON.stringify(calcParams) + "}")
            .then(querySucceeded)
            .fail(queryFailed);


    function postData(cp) {
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "/api/breeze/testCalc",
            data: cp,
            success: function (data) {
                alert(data);
                return data;
            },
            error: function (error) {
                jsonValue = jQuery.parseJSON(error.responseText);
            }
        });
    }

model:

Public Class calcParams2
    Public calcName As String
    Public calcNumber As Double
End Class

Server code:

<System.Web.Services.WebMethod()> _
Public Function testCalc(cp As calcParams2) As Double
    Dim c As New calcs
    Return cp.calcNumber
End Function

If I debug it, the "cp" parameter in the javascript postData function is:

"{calcParams2:{"calcName":"gwp_mat","components":124.332}}"

and yet in my web method, calcname is "nothing" and components is "0.0"

If I don't use JSON.Stringify I get a null object in my web method. I think I had one version of code where the calcName was passed but the components value was still zero. That was when I just called "return postData(JSON.stringify(calcParams))" I think.

1 Answer 1

1

You only need to send the object parameters in the JSON:

return postData(JSON.stringify(calcParams)) ...

However your JQuery Object needs to have the same parameter names as your Server object. Change your server object to be:

Public Class calcParams2
    Public calcName As String
    Public components As Double
End Class

OR change your client object:

var calcParams = {}
calcParams.calcName="gwp_mat";
calcParams.calcNumber=124.332;
Sign up to request clarification or add additional context in comments.

2 Comments

jQuery does all the encoding/stringifying for you - you can pass the object directly. In addition to making the server side edits as suggested, the OP should just be doing this: return postData({calcParams2:calcParams})
Doh! I created "calcparams2" as a copied test one and forgot to change the second property name. Man, that was dumb. However... Only Jonny S's answer works. I have to have the JSON.stringify call in there or else the web method receives nothing. I tried with ({calcParams2: calcParams}) and nothing got through. For some reason I need "stringify" in there for it to work. I have no idea why...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.