jQuery的AJAX和JSON格式 [英] jquery AJAX and json format

查看:85
本文介绍了jQuery的AJAX和JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web服务预计将收到的JSON,像这样:

  {FIRST_NAME:测试,姓氏:睾丸,电子邮件:moi@someplace.com,手机:+ 44 22 2222 2222,密码:测试}
 

在我的jQuery的Ajax调用:

  $。阿贾克斯({
        键入:POST,
        网址:hb_base_url +消费者,
        的contentType:应用/ JSON
        数据类型:JSON,
        数据: {
            FIRST_NAME:$(#namec)VAL()。
            姓氏:$(#surnamec)VAL()。
            电子邮件:$(#emailc)VAL()。
            手机:$(#numberc)VAL()。
            密码:$(#passwordc)VAL()。
        },
        成功:函数(响应){
            的console.log(响应);
        },
        错误:函数(响应){
            的console.log(响应);
        }
    });
 

有没有什么方法可以检查在我的数据正在发送的格式?我认为不会发送正确的JSON的服务器(即在确认的第一步)。

是我的jQuery code发送有效的JSON还是我错过了什么?

解决方案

您没有实际发送的JSON。您传递一个对象作为数据,但你需要字符串化的对象,并把这个字符串代替。

数据类型:JSON只告诉jQuery的,你想让它解析返回的JSON,这并不意味着jQuery将自动字符串化请求数据

更改为:

  $。阿贾克斯({
        键入:POST,
        网址:hb_base_url +消费者,
        的contentType:应用/ JSON
        数据类型:JSON,
        数据:JSON.stringify({
            FIRST_NAME:$(#namec)VAL()。
            姓氏:$(#surnamec)VAL()。
            电子邮件:$(#emailc)VAL()。
            手机:$(#numberc)VAL()。
            密码:$(#passwordc)VAL()。
        }),
        成功:函数(响应){
            的console.log(响应);
        },
        错误:函数(响应){
            的console.log(响应);
        }
});
 

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

{"first_name":"test","last_name":"teste","email":"moi@someplace.com","mobile":"+44 22 2222 2222", "password":"testing"}

My ajax call in jquery:

$.ajax({
        type: "POST",
        url: hb_base_url + "consumer",
        contentType: "application/json",
        dataType: "json",
        data: {
            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);
        }
    });

Is there any way to check the format in which my data is being sent? I'm supposedly not sending correct json to the server (that is the first step in validation).

Is my jquery code sending valid json or did I miss something?

解决方案

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

这篇关于jQuery的AJAX和JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆