1

I have a JSON object like so:

var newContact = {"contact_name":"Joe","contact_cell_phone":"4651354"}

And I need to pass it through to C# ASP backend using Angular $http.post such as:

$scope.Add = function (newContact) {

        // I am passing in this string variable "newContact_string", not the function param "newContact"
        var newContract_string = JSON.stringify(newContact);

        $http
            .post('Default.aspx/AddDb_contact', { newContact_string: newContact_string })
            .success(function (data) {
                console.log("New contact was added with id: " + data.d);
            })
            .error(function () {
                console.log("An error occurred adding the new contact.");
            });
    };

However, I am hit with this error in the JS console, specifically referring to this line of code:

ReferenceError: newContact_string is not defined
>> $http.post('Default.aspx/AddDb_contact', { newContact_string: newContact_string })

My C# backend code is never reached:

[WebMethod]
public static object AddDb_contact(string newContact_string)
{
    return newContact_string;
}

Perhaps it is a syntax error? How would I go about AJAXing the Angular way?

1 Answer 1

1

You have a typo.

You declare var newContract_string = JSON.stringify(newContact);

but you pass newContact_string.

You need: { newContact_string: newContract_string }

Sign up to request clarification or add additional context in comments.

1 Comment

@KangzeHuang another set of eyes is always helpful :).

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.