Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将参数添加到post方法的ajax调用中_Jquery_Ajax - Fatal编程技术网

Jquery 如何将参数添加到post方法的ajax调用中

Jquery 如何将参数添加到post方法的ajax调用中,jquery,ajax,Jquery,Ajax,我有html、jquery和json。在我的示例中,我从一个简单的json和ajax调用中获取所有值。在Submit按钮上,我根据我的要求将文本框值传递给我的ajax调用url。但我使用的是post方法。它工作正常。这是通过post方法传递参数的正确方法,还是我需要以其他方式传递参数参数。下面是代码 HTML 1.json 您可以使用数据属性 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8"

我有html、jquery和json。在我的示例中,我从一个简单的json和ajax调用中获取所有值。在Submit按钮上,我根据我的要求将文本框值传递给我的ajax调用url。但我使用的是post方法。它工作正常。这是通过post方法传递参数的正确方法,还是我需要以其他方式传递参数参数。下面是代码

HTML 1.json
您可以使用数据属性

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "1.json?id="+x,
    data:{post_variable_name1:value1,post_variable_name2:value2},
    success: function(result) {
    }
});

你看过文件了吗?特别是
数据
属性
$(document).ready(function () {
     $("#submitbtn").click(function(){
     var x = $("#text1").val();
     alert(x);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "1.json",
        data: { id : x },
        success: function(result) {
            $("#name1").text(result.critical[0].name);
            $("#value1").text(result.critical[0].value);

            $("#name2").text(result.major[0].name);
            $("#value2").text(result.major[0].value);

            $("#name3").text(result.minor[0].name);
            $("#value3").text(result.minor[0].value);
        }
    });
     });
});
{
    "critical": [{
        "name": "critical",
        "value": "50"
    }],
    "major": [{
        "name": "major",
        "value": "40"
    }],
    "minor": [{
        "name": "minor",
        "value": "20"
    }]
}
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "1.json?id="+x,
    data:{post_variable_name1:value1,post_variable_name2:value2},
    success: function(result) {
    }
});