Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Javascript jQuery:使用参数发布ajax请求的正确方法是什么?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript jQuery:使用参数发布ajax请求的正确方法是什么?

Javascript jQuery:使用参数发布ajax请求的正确方法是什么?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我的代码由一个对服务器的ajax请求组成,其中有要添加到请求url的参数。在jQuery中实现哪种方法是正确的 var param ="roomNumber="+this.roomNumber+"&roomId="+this.roomId+"&UniqueId="+this.surveyData[this.currentIndex].id+"&optionId="+optId; 或 json数据是否正确?在我的示例中,我使用json数据格式。您可以使用服务器接受的任何选

我的代码由一个对服务器的ajax请求组成,其中有要添加到请求url的参数。在jQuery中实现哪种方法是正确的

var param ="roomNumber="+this.roomNumber+"&roomId="+this.roomId+"&UniqueId="+this.surveyData[this.currentIndex].id+"&optionId="+optId;


json数据是否正确?

在我的示例中,我使用json数据格式。您可以使用服务器接受的任何选项

$.ajax({
    url: 'http://someUrl',
    contentType: 'application/json',
    data: {
        param1: '345345',
        param2: 23523
    }
}).done(function(result){
    //success
}).fail(function(e){
    //error
}); 

你能解释一下查询字符串和json对象中的开销吗?如果我理解正确的话。。。您已经准备好
这个
对象,其中包含需要发送的参数。因此,您可以将
这个
对象先前分配给某个变量,然后将其设置为ajax请求的
数据
参数值。在$.ajax()之前,使:
var dataObj=this。在
$.ajax({…,数据:dataObj,…})中和url将像下面这样干净:
http://address.com
,不像你有
http://address.com?param1=val¶m2=val&.......