jquery AJAX and json format

前端 未结 4 1395
有刺的猬
有刺的猬 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);
            }
        });
    
    0 讨论(0)
  • 2020-11-30 09:05

    You aren't actually sending JSON. You are passing an object as the data, but you need to stringify the object and pass the string instead.

    Your dataType: "json" only tells jQuery that you want it to parse the returned JSON, it does not mean that jQuery will automatically stringify your request data.

    Change to:

    $.ajax({
            type: "POST",
            url: hb_base_url + "consumer",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({
                first_name: $("#namec").val(),
                last_name: $("#surnamec").val(),
                email: $("#emailc").val(),
                mobile: $("#numberc").val(),
                password: $("#passwordc").val()
            }),
            success: function(response) {
                console.log(response);
            },
            error: function(response) {
                console.log(response);
            }
    });
    
    0 讨论(0)
  • 2020-11-30 09:09
    $.ajax({
       type: "POST",
       url: hb_base_url + "consumer",
       contentType: "application/json",
       dataType: "json",
       data: {
           data__value = JSON.stringify(
           {
               first_name: $("#namec").val(),
               last_name: $("#surnamec").val(),
               email: $("#emailc").val(),
               mobile: $("#numberc").val(),
               password: $("#passwordc").val()
           })
       },
       success: function(response) {
           console.log(response);
       },
       error: function(response) {
           console.log(response);
       }
    });
    

    (RU) На сервере ваши данные можно получить как - $_POST['data__value']; Например для получения значения first_name на сервере, нужно написать:

    (EN) On the server, you can get your data as - $_POST ['data__value']; For example, to get the first_name value on the server, write:

    $test = json_decode( $_POST['data__value'] );
    echo $test->first_name;
    
    0 讨论(0)
  • 2020-11-30 09:16

    Currently you are sending the data as typical POST values, which look like this:

    first_name=somename&last_name=somesurname
    

    If you want to send data as json you need to create an object with data and stringify it.

    data: JSON.stringify(someobject)
    
    0 讨论(0)
提交回复
热议问题