jquery post方法无效

时间:2018-04-05 07:51:56

标签: javascript jquery

我使用jquery post方法提交一个简单的表单,但是当我调用post方法时,我收到了这个错误的错误信息

VM1233 jquery.min.js:2 Uncaught TypeError: (h.dataType || "*").toLowerCase is not a function
    at Function.ajax (VM1233 jquery.min.js:2)
    at Function.w.(:8000/Marketing/anonymous function) [as post] (https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js:2:78094)
    at submitLeadDetails (VM1235 main.js:10)
    at HTMLFormElement.onsubmit (index.html?name=haskdaskd&Contact=harish+kumar&phone=9030590437&date=12%2F04%2F2018&Time=09%3A00:33)

这是我写的代码:

var submitLeadDetails = function(){
    console.log('Post function executed');
    var LeadDetailsObject = {};
    LeadDetailsObject.schoolName = document.getElementById('name').value;
    LeadDetailsObject.contactPerson = document.getElementById('contact').value;
    LeadDetailsObject.contactPersonPhoneNumber =document.getElementById('phone').value;
    LeadDetailsObject.date = document.getElementById('date').value;
    LeadDetailsObject.time = document.getElementById('time').value;
    console.log(LeadDetailsObject);
    $.post('http://localhost:8000/webapi/leads', LeadDetailsObject, function(data){
        console.log('Success');
        console.log(data);
        $('#leadDetailsForm')[0].reset();
    }, function(error){
        console.log('Error');
        console.log(error);
    })
}
      <form id="leadDetailsForm" onsubmit="submitLeadDetails()">
                <div class="form-group">
                    <label for="name">School Name:</label>
                    <input type="text" class="form-control" id="name" placeholder="Enter School Name" name="name" required>
                </div>

                <div class="form-group">
                    <label for="contact">Contact Person:</label>
                    <input type="text" class="form-control" id="contact" placeholder="Enter Contact Person Name" name="Contact" required>
                </div>
                <div class="form-group">
                    <label for="phone">Phone Number:</label>
                    <input type="number" class="form-control" id="phone" placeholder="Enter Contact Person's Phone Number" name="phone" required>
                </div>
                <!--<div class="form-group">-->
                    <!--<label for="address">Address:</label>-->
                    <!--<textarea class="form-control" rows="5" placeholder="Enter School Address" id="address" required></textarea>-->
                <!--</div>-->
                <div class="form-group col-sm-6" style="padding-left: 0;">
                    <label for="date">Date:</label>
                    <input type="text" class="form-control" id="date" placeholder="Enter Date" name="date" required>
                </div>
                <div class="form-group col-sm-6">
                    <label for="time">Time:</label>
                    <input type="text" class="form-control" id="time" placeholder="Enter Time" name="Time" required>
                </div>
                <button type="submit"  class="btn  btn-lg pull-right" style="margin-top: 3%; background-color: #2a2c30; color:#fff;" >
                    Submit
                </button>
            </form>
      

2 个答案:

答案 0 :(得分:0)

错误说您将错误的参数传递给.post()方法。第四个参数应该是dataType,而不是回调。

请参阅文档:https://api.jquery.com/jquery.post/

在您的情况下,您可以使用.done().fail()来处理请求的状态:

var submitLeadDetails = function(){
    console.log('Post function executed');
    var LeadDetailsObject = {};
    LeadDetailsObject.schoolName = document.getElementById('name').value;
    LeadDetailsObject.contactPerson = document.getElementById('contact').value;
    LeadDetailsObject.contactPersonPhoneNumber =document.getElementById('phone').value;
    LeadDetailsObject.date = document.getElementById('date').value;
    LeadDetailsObject.time = document.getElementById('time').value;
    console.log(LeadDetailsObject);
    $.post('http://localhost:8000/webapi/leads', JSON.stringify(LeadDetailsObject))
      .done(function(data){
        console.log('Success');
        console.log(data);
        $('#leadDetailsForm')[0].reset();
      })
      .fail(function(xhr, status, error) {
        console.log('Error');
        console.log(error);
      });
}

答案 1 :(得分:0)

jQuery可能由于某些检查而试图将值小写。但是,当它尝试将数字小写时,它就会崩溃。输入中有type="number"。可能导致错误