Pregunta

I've built a test client page and I'm trying to post to a Web API that I've built, but I get the following error:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'val'

on the following code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="Scripts/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnSubmit").click(function () {
            var user = {
                email: ('#txtEmail').val(),
                password: ('#txtPassword').val(),
                firstName: ('#txtFirstName').val(),
                lastName: ('#txtLastName').val(),
                country: ('#txtCountry').val(),
                postal: ('#txtPostal').val()
            };
            $.post("http://myUrl/api/****/***************", user, function (data, status) {
                alert("Data: " + data + "\nStatus: " + status);
            });
        });
    });
</script>

I can hit the service in Fiddler with the intended result, so I don't think its my service.

Also, I found a question on Stack talking about a similar issue regarding changing:

<script type="text/javascript" src="Scripts/jquery-2.1.1.min.js"></script>

to

<script type="text/javascript" src="Scripts/jquery-2.1.1.min.js" />

but when I try this, I get an intellisense error in Visual Studio on that line wanting to change it back to the original line of code.

I'm still getting familiar with jquery, so that skillset isn't that strong yet. I know I'm missing something, but not sure what. If anyone could shed some light on the issue, I'd appreciate it.

¿Fue útil?

Solución

you are missing $:

email: $('#txtEmail').val()

it should be like:

 var user = {
                email: $('#txtEmail').val(),
                password: $('#txtPassword').val(),
                firstName: $('#txtFirstName').val(),
                lastName: $('#txtLastName').val(),
                country: $('#txtCountry').val(),
                postal: $('#txtPostal').val()
            };
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top