通过调用带参数的URL来获取json对象 [英] Get json object by calling a URL with parameters

查看:104
本文介绍了通过调用带参数的URL来获取json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,但我有一个编码器的心理障碍:

This seems like a simple problem but I have a coder's mental block:

概念:

我输入一个网址,即 - www.mysite.com/getStuff?name=Jerry&occupation=Engineer&Id=12345

I type a URL, i.e - www.mysite.com/getStuff?name=Jerry&occupation=Engineer&Id=12345

获取一个网页或者我想要取回一个json对象的东西,以便我可以在另一个页面上解析。

and instead of getting back a webpage or something I want to get back a json object so that I can parse on a different page.

捕获:

我当然可以通过使用这些参数调用MVC控制器并返回一个json对象来实现这一点。但是,假设我需要在一个js文件中创建这个json对象,该文件从URL获取这些参数的值,然后我将json作为结果返回。

I can certainly accomplish this by calling a MVC controller with those parameters and returning a json object. However, Let's say I need to create this json object inside a js file that takes those parameters' values from the URL and I get my json back as the result.

问题

我可以将参数传递给js文件并返回json对象?或
我可以从控制器调用js文件并将这些参数传递给并检索json对象吗?

我是否需要通过URL调用控制器,或者我可以调用一个js文件从URL给它参数然后返回json?

Can I pass parameters to a js file and return a json object? Or Can I call a js file from a controller and pass it these parameters to and retrieve a json object?
Do I even need to call a controller via a URL, or can I just call a js file giving it parameters from a URL and then returning the json?

使用MVC,js,jquery来处理这个场景的正确/最佳方法是什么? .anything ??

What is the proper/best way of handling this scenario, with MVC, js, jquery...anything??

非常感谢大家!

推荐答案

使用jquery通过在创建json对象之前将其插入< script> 标记来解析URL。来自LekisS的链接

Use jquery to parse the URL by inserting this into a <script> tag before creating the json object. from link from LekisS

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

// Get object of URL parameters
var allVars = $.getUrlVars();

// Getting URL var by its nam
var byName = $.getUrlVar('name');

在单独的脚本标记中创建json对象。您需要包含 Json2.js 插件才能将对象转换为JSON。因此,在创建JSON对象之前也要包含此脚本。

In a separate script tag create your json object. You will need to include the Json2.js plugin to convert objects to JSON. So include this script also before the JSON object creation.

一旦有了相应的脚本和变量,就可以根据需要使用这些参数创建一个json对象,方法是调用它们,如图所示在使用jquery的示例的底部。您还可以从Json2.js脚本文件中查找所需的JSON转换(即字符串或对象)。

Once you have the appropriate scripts and variables you can create a json object using those parameters as needed by calling them as shown at the bottom of the example using jquery. You can also look up which JSON conversion (i.e, to string or object) you want from the Json2.js script file.

现在我们将所有内容放在一堆脚本中但是我们在哪里通过URL调用获取json对象?

Now we have everything inside a bunch of scripts but where do we get json object through URL calling?

所以答案很简单:

使用这些脚本创建一个简单的html页面,最后一个脚本最终创建并返回json。上传到服务器并使用URL参数,例如

Create a simple html page with these scripts where the last script finally creates and returns the json. Upload to server and use URL parameters like

www.mysite.com/getStuff?para1=value&para2=value2 获取json对象。

这篇关于通过调用带参数的URL来获取json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆