Put method not working in Google Chrome through AJ

2019-03-04 15:16发布

I have this working fine in Mozilla and IE but for some reason not chrome. In chrome, the error callback executes every time returning an error code of zero. Lots of articles on Stackoverflow keep reiterating how all major browsers support the "PUT" method through AJAX instead of forms. Chrome appears to be the exception.....

JavaScript

     function works(){alert("working");} // just a success callback

    $(document).ready(function(){

    $("#crudForm").submit(function(){
        $.ajax({url:"/UtilityDashboard/MeasurementNodes",
            data:parseFormData("crudForm"),
            cache: "false",
            async: "false",
            dataType: "text",
            contentType: "application/x-www-form-urlencoded",
            type:"put",
            success: works(),
            error:function(xhr){alert(xhr.status + xhr.statusText);} });
    });      

    });

HTML

    <form id="crudForm">
       Name<BR/>
       <input type="text" name="name"/><BR/><BR/>
       Node Id<BR/>
       <input type="text" name="node_id"/><BR/><BR/>
       Type<BR/> 
       <input type="text" name="type"/><BR/><BR/>
       Parent<BR/> 
       <input type="text" name="parent_id"/><BR/><BR/>
       Longitude<BR/> 
       <input type="text" name="longitude"/><BR/><BR/>
       Latitude<BR/>
       <input type="text" name="latitude"/><BR/><BR/>           
       Description<BR/>          
       <textarea name="description" rows="5" cols="40">Insert description of measurement node here</textarea><BR/><BR/>           
       <input type="submit" value="Add Node"/>

    </form>

1条回答
趁早两清
2楼-- · 2019-03-04 15:38

So Eugene and OhgodWhy were correct. Chrome does have support for the "put" method in the XMLHTTPRequest object!

Solution

The problem I was experiencing went away when I stopped using the "submit" event to transmit my form data, and instead relied on the "click" event. This took care of some other issues I was having where after submit chrome would append the form variables to the current URL. This could probably have also been solved by using event.preventDefault.

查看更多
登录 后发表回答