jquery AJAX and json format

前端 未结 4 1407
有刺的猬
有刺的猬 2020-11-30 09:00

I have a webservice that expects to receive json, like so:

{\"first_name\":\"test\",\"last_name\":\"teste\",\"email\":\"moi@someplace.com\",\"mobile\":\"+44          


        
4条回答
  •  醉酒成梦
    2020-11-30 09:02

    I never had any luck with that approach. I always do this (hope this helps):

    var obj = {};
    
    obj.first_name = $("#namec").val();
    obj.last_name = $("#surnamec").val();
    obj.email = $("#emailc").val();
    obj.mobile = $("#numberc").val();
    obj.password = $("#passwordc").val();
    

    Then in your ajax:

    $.ajax({
            type: "POST",
            url: hb_base_url + "consumer",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(obj),
            success: function(response) {
                console.log(response);
            },
            error: function(response) {
                console.log(response);
            }
        });
    

提交回复
热议问题