本文介绍了何时在ajax调用中使用JSON.stringify()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ajax和JSON的了解是有限的,但是我知道在ajax调用中使用JSON.stringify有时会很有用.我在下面的ajax调用工作正常,而在其下面的带有stringify方法的调用不起作用.我想知道我是否正确使用.stringify,如果不正确,什么时候应该在ajax中使用JSON.stringify.我正在将MVS与模型,视图和控制器一起使用.

My knowledge of ajax and JSON is limited, but I know that using JSON.stringify in an ajax call can sometimes be useful. I have an ajax call below that works fine, while the one below it with the stringify method that does not work. I am wondering if I am using .stringify correctly, and if not, when should I use JSON.stringify in ajax, if ever. I am using MVS with a model, view, and controller.

这是我通常进行ajax调用的方式以及构建url部分的方式.

This is how I usually do ajax calls, and how i build the url portion.

    function AddEquipment(id, name, type, description, email) {
        $.ajax({
            url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id +
                "&name=" + name + "&type=" + type + "&description=" +
                description + "&email=" + email,
            type: "GET",
            cache: false,
            datatype: "JSON",
            success: function(result) {
                //do stuff
            }
        });
    }

下面,我尝试使用JSON.stringify而不是手动构建整个url,但这不起作用.

Below I have tried using JSON.stringify instead of building the entire url manually, and it does not work.

    function AddEquipment(id, name, type, description, email) {
        $.ajax({
            url: '@Url.Action("AddEquipment", "Home")',
            type: "GET",
            cache: false,
            datatype: "JSON",
            data: JSON.stringify({
                "id": id,
                "name": name,
                "type": type,
                "description": description,
                "email": email
            }),
            success: function(result) {
                //do stuff
            }
        });
    }

与此一起使用的控制器方法将id接受为一个int,而其他所有内容都是一个字符串.我以前使用JSON.stringify混合变量(整数,布尔值,字符串)没有问题.

the controller method this goes with accepts id as an int, while everything else is a string. I have used JSON.stringify before with mixed variables (ints, bools, strings) without an issue.

任何有用的信息将不胜感激,

Any helpful information is greatly appreciated,

谢谢!

推荐答案

这是两个不同的字符串(它们最终得出的值).一个不等于另一个. Stringify不会根据需要生成'='.

These are two different strings (values that they eventually evaluate to be). One is not equal to other. Stringify will not yield you ' = ' as you require.

阅读此发布以在其上传递数据接到电话

Read this post to pass data on a get call

JSON.stringify({
                "id": id,
                "name": name,
                "type": type,
                "description": description,
                "email": email
            }),

url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id +
                "&name=" + name + "&type=" + type + "&description=" +
                description + "&email=" + email 

这篇关于何时在ajax调用中使用JSON.stringify()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:09