简体   繁体   English

发送从jQuery Ajax中的字符串收集的多个数据参数

[英]Send multiple data params collected from string in jQuery Ajax

I have a string: 我有一个字符串:

'{format: "json", user: "user", password: "password"}' '{格式:“ json”,用户:“ user”,密码:“ password”}'

and I want to send all this data using jQuery's AJAX. 我想使用jQuery的AJAX发送所有这些数据。 I've tried this way (requestData['data'] is the string) : 我已经尝试过这种方式(requestData ['data']是字符串):

$.ajax({
     url: requestData['url'],
     type: requestData['type'],
     data: requestData['data'],
     error: function(xhr) {
         alert("failed");
     },
     dataType: 'json',
     success: function(data, textStatus, xhr) {
        alert("success");
     }
});

Do I have to encode the string somehow? 我是否必须以某种方式编码字符串?

You can send the whole object, its not a problem: 您可以发送整个对象,这不是问题:

var jsonObj = {format: "json", user: "user", password: "password"};

$.ajax({
     url: requestData['url'],
     type: jsonObj,
     data: requestData['data'],
     error: function(xhr) {
         alert("failed");
     },
     dataType: 'json',
     success: function(data, textStatus, xhr) {
        alert("success");
     }
});
var datum = {
   format: "json",
   user: "user",
   password: "password"
};

$.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: url,          // your url
   dataType: "json",
   data: JSON.stringify(datum),
   success: function(response) {
      var result = response;
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM