使用 Jquery $getJSON 如何动态为 Url 参数后的 [data] 参数创建数据?

发布于 2024-08-25 05:49:24 字数 497 浏览 5 评论 0原文

我没有任何问题让 Json 工作并解析 json 返回。我只是想知道如何构建一个动态的“无论数据是什么”并将其粘贴到 [data] 中以从那里传递我的参数,而不是手动将它们附加到 url 中。

来自 jquery 网站的示例:

$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){
     alert("JSON Data: " + json.users[3].name);
});

我认为我可以构建一个字符串(无论如何都没有意义)并将其放入 { } 内,但我显然不理解那部分。

name: 不是字符串,您不能在该部分中放置变量,那么我如何动态地将项目放入 [data] 中。

更新:感谢您的回答。如果我稍后没有找到有效值,我是否能够从对象中删除参数名称,或者我是否需要销毁它并重新创建它?我正在使用一些选择框来选择内容,但我可能没有选择某些内容,因此我不想传递该参数名称/值。

I have no problems getting the Json to work and parse the json return. I was just wondering how I could build a dynamic "whatever data is" and stick it into [data] to pass my parameters from there and not manually append them to the url.

From jquery website example:

$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){
     alert("JSON Data: " + json.users[3].name);
});

I thought I could build a string ( which doesn't make sense anyway ) and drop it inside the { }, but I obviously don't understand that part.

name: isn't a string and you can't put a variable in that part, so how would I dynamically put items into whatever [data] is.

UPDATE: Thanks for the answers. Would I be able to remove a parameter name from the object if I didn't find a valid value later on or would I need to destroy it and recreate it? I am using some select boxes to choose things and I may not have something selected so I wouldn't want to pass that parameter name/value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城追梦 2024-09-01 05:49:25

{} 是一个 javascript 对象文字,因此 name 是那个物体。

你可以做这样的事情:

var data = {};
data.name = $("#input").val();
$.getJSON("test.js", data, function(json){
     alert("JSON Data: " + json.users[3].name);
});

或者这样的事情

var data = {
    name: $("#input").val(),
};
$.getJSON("test.js", data, function(json){
     alert("JSON Data: " + json.users[3].name);
});

The {} is a javascript object literal so name is a property of that object.

You can do something like this:

var data = {};
data.name = $("#input").val();
$.getJSON("test.js", data, function(json){
     alert("JSON Data: " + json.users[3].name);
});

Or something like this

var data = {
    name: $("#input").val(),
};
$.getJSON("test.js", data, function(json){
     alert("JSON Data: " + json.users[3].name);
});
飞烟轻若梦 2024-09-01 05:49:24

您可以像这样构建您的对象:

var someVar = someRandomString();
$.getJSON('test.js', (function() {
  var theData = {};
  theData[someVar] = someRandomValue();
  return theData;
})(), function(jsonStuffFromServer) {
  alert("JSON Data: " + jsonStuffFromServer.users[3].name);
});

当然,您不必在匿名函数中构建“数据”对象;;您可以事先单独执行此操作:

var theData = {};
theData[someVariableWithANameInIt] = someRandomValue();
$.getJSON(url, theData, function(jsonStuff) { ... });

以下是如何从页面上的一组

var theData = {};
$('#myForm select').filter(function() { return $(this).val() != ''; })
  .each(function() {
    theData[this.name] = $(this).val();
  });

$.getJSON(url, theData, function(jsonStuff) { ... });

You can build your object like this:

var someVar = someRandomString();
$.getJSON('test.js', (function() {
  var theData = {};
  theData[someVar] = someRandomValue();
  return theData;
})(), function(jsonStuffFromServer) {
  alert("JSON Data: " + jsonStuffFromServer.users[3].name);
});

Of course you don't have to build up the "data" object right there in an anonymous function; you can do it separately beforehand:

var theData = {};
theData[someVariableWithANameInIt] = someRandomValue();
$.getJSON(url, theData, function(jsonStuff) { ... });

Here's how you could build up such an object from a set of <select> elements on your page:

var theData = {};
$('#myForm select').filter(function() { return $(this).val() != ''; })
  .each(function() {
    theData[this.name] = $(this).val();
  });

$.getJSON(url, theData, function(jsonStuff) { ... });
浮光之海 2024-09-01 05:49:24

创建一个对象,然后您可以在其中放入任何您喜欢的属性:

var data = new Object();

或者:

var data = {};

您可以使用属性语法:

data.name = 'John';
data.time = '2pm';

或关联数组语法(如果参数名称是关键字,这很有用):

data['name'] = 'John';
data['time'] = '2pm';

您当然可以使用变量作为值,甚至使用变量来指定参数名称:

var param = 'name';
var value = 'John';
data[param] = value;

然后您只需在调用中使用数据变量:

$.getJSON("test.js", data, function(json){
   alert("JSON Data: " + json.users[3].name);
});

Create an object, then you can put any properties you like in it:

var data = new Object();

or:

var data = {};

You can use property syntax:

data.name = 'John';
data.time = '2pm';

or associate array syntax (which is useful if the parameter name is a keyword):

data['name'] = 'John';
data['time'] = '2pm';

You can of course use variables for the values, and even use variables to specify the parameter name:

var param = 'name';
var value = 'John';
data[param] = value;

Then you just use the data variable in the call:

$.getJSON("test.js", data, function(json){
   alert("JSON Data: " + json.users[3].name);
});
鸠魁 2024-09-01 05:49:24

你可以这样做:

var myName = $("#nameField").val();
var myTime = $("#timeField").val();

$.getJSON("test.js", { 'name': myName, 'time': myTime}, function(json){
     alert("JSON Data: " + json.users[3].name);
});

是你的意思吗,还是动态参数以及变量?

You can do this:

var myName = $("#nameField").val();
var myTime = $("#timeField").val();

$.getJSON("test.js", { 'name': myName, 'time': myTime}, function(json){
     alert("JSON Data: " + json.users[3].name);
});

Is that what you mean, or dynamic parameters as well as variables?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文