如何通过 URL 访问 html 内容 json 对象
How to access an html content json object via URL
例如,如果我的 URL 是 www.someurlsomewhere.com/GetItemById?Id=5 并且我的响应是一个 json 对象数组。
Success: true
Html: "<html><body>test</body></html>"
FileName: "test.html"
如何说 www.someurlsomewhere.com/GetItemById?Id=5?data=Html 使其像 link 到 Html响应中我的 json 对象的一部分。
您正在谈论查询参数或 URL 参数。添加多个参数的正确格式是用 &
分隔它们。您的 URL 看起来类似于:www.someurlsomewhere.com/GetItemByID?Id=5&src=html
.
要提取该信息,您需要解析 URL 参数,然后根据数据提供您想要的信息。这可以在服务器端或客户端完成。查找 URL Parameter Parsing 以获取有关如何使用您选择的语言执行此操作的想法。 JavaScript 中出现的示例之一是 How can I get query string values in JavaScript?。
解析出所需的 URL 参数后,现在需要将其呈现到页面。查找解析 HTML。我假设你在 javascript 中这样做,只是为了给你一个解析的例子:
var data = {
success: true,
html: "<html><body>test</body></html>",
filename: "test.html"
}
var el = document.createElement('html'); //creates a temporary dummy element to append to the page... although if you already have something on the page, you may use that container
el.innerHTML = data.html; //here you're selecting the element and adding a string of HTML to it
你问的问题有很多未知数。但这是一个潜在的客户端解决方案,它检索 URL 参数,然后将其作为 HTML 传递给 DOM.
<script>
//Assuming your URL looks like this:
// www.someurlsomewhere.com/GetItemByID?Id=5&src=html
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var src = getParameterByName('src'); //Use your parameter function to retrieve the src parameter from the URL. Currently src = 'html'
//This is a representation of your JSON payload that you're receiving from somewhere
var data = {
success: true,
html: "<html><body>test</body></html>",
filename: "test.html"
}
var el = document.createElement('html'); //creates a temporary dummy element to append to the page... although if you already have something on the page, you may use that container
el.innerHTML = data[src]; //here you're selecting the element and adding a string of HTML to it. This would translate to data.html
</script>
例如,如果我的 URL 是 www.someurlsomewhere.com/GetItemById?Id=5 并且我的响应是一个 json 对象数组。
Success: true
Html: "<html><body>test</body></html>"
FileName: "test.html"
如何说 www.someurlsomewhere.com/GetItemById?Id=5?data=Html 使其像 link 到 Html响应中我的 json 对象的一部分。
您正在谈论查询参数或 URL 参数。添加多个参数的正确格式是用 &
分隔它们。您的 URL 看起来类似于:www.someurlsomewhere.com/GetItemByID?Id=5&src=html
.
要提取该信息,您需要解析 URL 参数,然后根据数据提供您想要的信息。这可以在服务器端或客户端完成。查找 URL Parameter Parsing 以获取有关如何使用您选择的语言执行此操作的想法。 JavaScript 中出现的示例之一是 How can I get query string values in JavaScript?。
解析出所需的 URL 参数后,现在需要将其呈现到页面。查找解析 HTML。我假设你在 javascript 中这样做,只是为了给你一个解析的例子:
var data = {
success: true,
html: "<html><body>test</body></html>",
filename: "test.html"
}
var el = document.createElement('html'); //creates a temporary dummy element to append to the page... although if you already have something on the page, you may use that container
el.innerHTML = data.html; //here you're selecting the element and adding a string of HTML to it
你问的问题有很多未知数。但这是一个潜在的客户端解决方案,它检索 URL 参数,然后将其作为 HTML 传递给 DOM.
<script>
//Assuming your URL looks like this:
// www.someurlsomewhere.com/GetItemByID?Id=5&src=html
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var src = getParameterByName('src'); //Use your parameter function to retrieve the src parameter from the URL. Currently src = 'html'
//This is a representation of your JSON payload that you're receiving from somewhere
var data = {
success: true,
html: "<html><body>test</body></html>",
filename: "test.html"
}
var el = document.createElement('html'); //creates a temporary dummy element to append to the page... although if you already have something on the page, you may use that container
el.innerHTML = data[src]; //here you're selecting the element and adding a string of HTML to it. This would translate to data.html
</script>