javascript - Object doesn't support property or method 'val' on ajax POST to Web Api - Stack Overflow

I've built a test client page and I'm trying to post to a Web API that I've built, but I

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="">
<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.

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/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.

Share Improve this question asked May 15, 2014 at 15:13 Rex_CRex_C 2,4777 gold badges36 silver badges56 bronze badges 2
  • 1 you're missing the $... eg: $('#txtEmail').val(),etc – Moob Commented May 15, 2014 at 15:14
  • You're missing the $ from your selectors, eg: email: $('#txtEmail').val() – Rory McCrossan Commented May 15, 2014 at 15:14
Add a ment  | 

1 Answer 1

Reset to default 5

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()
            };

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745540518a4632106.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信