我正在尝试将浏览器定向到其他页面.如果我想要一个GET请求,我可能会说
document.location.href = 'http://example.com/q=a';
Run Code Online (Sandbox Code Playgroud)
但是我试图访问的资源不会正常响应,除非我使用POST请求.如果这不是动态生成的,我可能会使用HTML
<form action="http://example.com/" method="POST">
<input type="hidden" name="q" value="a">
</form>
Run Code Online (Sandbox Code Playgroud)
然后我只需从DOM提交表单.
但实际上我想要允许我说的JavaScript代码
post_to_url('http://example.com/', {'q':'a'});
Run Code Online (Sandbox Code Playgroud)
什么是最好的跨浏览器实现?
编辑
对不起,我不清楚.我需要一个改变浏览器位置的解决方案,就像提交表单一样.如果使用XMLHttpRequest可以实现这一点,那就不明显了.这不应该是异步的,也不应该使用XML,所以Ajax不是答案.
我总是想知道为什么这个对象会被这样调用?
您的请求正文不需要是XML格式.此外,从服务器接收的数据可以作为JSON,XML,HTML或纯文本提取.XML在此对象中不起重要作用.这有点陈词滥调吗?这个对象在第一次创建时曾经是什么?
我想写一个javascript函数,它返回HTML内容作为字符串给定的函数URL.我在Stackoverflow上找到了类似的答案.
我试图用这个答案来解决我的问题.
然而,似乎document.write()没有写任何东西.当我加载页面时,我得到一个空白屏幕.
<html>
<head>
</head>
<body>
<script type="text/JavaScript">
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
document.write(httpGet("https://stackoverflow.com/"));
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个天气应用程序,显示一周中许多天的天气和温度.我目前正在使用openweathermap api进行此类任务,事情是我想要的信息(即天气的日期)仅以xml格式提供.由于我出于学术原因在ES6(ES2015)中重建它,我想也使用fetch api,但由于fetch方法解析它,它只是传递错误.所以我怎样才能获取它或者mby有更好的方法来实现它.
let apis = {
currentWeather: { //get user selected recomendation weather
api:"http://api.openweathermap.org/data/2.5/forecast/daily?lat=",
parameters: "&mode=xml&units=metric&cnt=6&APPID=/*api key*/",
url: (lat, lon) => {
return apis.currentWeather.api + lat + "&lon=" + lon +
apis.currentWeather.parameters
}
}
};
function getCurrentLoc() {
return new Promise((resolve, reject) => navigator.geolocation
.getCurrentPosition(resolve, reject))
}
function getCurrentCity(location) {
const lat = location.coords.latitude;
const lon = location.coords.longitude;
return fetch(apis.currentWeather.url(lat, lon))
.then(response => response.json())
.then(data => console.log(data))
}
getCurrentLoc()
.then( coords => getCurrentCity(coords))
Run Code Online (Sandbox Code Playgroud) 我有一个spring-boot应用程序,withemleaf.我反复更新页面,并将其重定向到同一页面,所以我希望页面的元素得到更新:
@GetMapping("/suggested-events/vote/{eventId}")
public String voteForEvents(Model model,
@PathVariable("eventId") Long eventId,
@RequestParam(value = "message", required = false) String message ) {
log.info("The message is: "+message);
SuggestedEvent event = suggestedEventService.findSuggestedEventById(eventId);
ArrayList<SuggestedEvent> events = suggestedEventService.findSuggestedEventsByArea(event.getArea());
model.addAttribute("mainEvent",event);
model.addAttribute("events",events);
model.addAttribute("message",message);
return "/suggested-event/vote";
}
Run Code Online (Sandbox Code Playgroud)
当按钮在视图中被按下时,它会触发以下post方法:
@PostMapping("/suggested-events/vote")
public String voteForASuggestedEvent(RedirectAttributes redirectAttributes){
log.info("You have made a vote");
redirectAttributes.addAttribute("message", "Success");
return "redirect:/suggested-events/vote/1";
}
Run Code Online (Sandbox Code Playgroud)
该第二控制器方法执行a的操作message,并将其重定向到第一方法.因此,它成功重定向到第一个方法并记录
log.info("The message is: "+message);
Run Code Online (Sandbox Code Playgroud)
但它没有刷新我的页面,我没有得到消息作为模型?
当我重定向到第一个方法时,我希望它添加message到我的模型:
model.addAttribute("message",message);
Run Code Online (Sandbox Code Playgroud)
但它没有添加到我的页面
当我访问这样的URL:http://my.url.com/file.txt时,浏览器将显示文本。
我想要一个简单的javscript命令,该命令可以执行以下操作:
1.转到url
3.获取屏幕上显示的文本
4.将其存储在变量中以进行进一步处理
所以像
var url = http: //my.url.com/file.txt;
Run Code Online (Sandbox Code Playgroud)
//这里有一些转到url的代码
//一些代码,其中包含上述信息并执行以下操作:
var fileInfo = ---content of file.txt---
Run Code Online (Sandbox Code Playgroud)
请注意,我从txt文件中查找的信息在html标记中
<p>Text I need in Variable</p>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
谢谢!
我在flask中有一个名为array的函数,它接受一个列表并打印出列表中的项目:
def array(list):
string = ""
for x in list:
string+= x
return string
Run Code Online (Sandbox Code Playgroud)
在客户端,我想将名为str的javascript数组传入此数组.我该怎么办?这就是我现在所拥有的,但Flask并未阅读添加的变量.有任何想法吗?
for (var i = 0; i < response.data.length; i++) {
console.log(i);
// str = str + "<br/><b>Pic</b> : <img src='"+ response.data[i].picture +"'/>";
str[i] = response.data[i].picture;
}
window.location = "{{ url_for('array', str=list ) }}";
Run Code Online (Sandbox Code Playgroud) 当按下按钮时,我在 html 文件中调用一个 javascript 函数,该函数将两个字符串作为参数(来自输入字段)。当调用该函数时,我想将这些参数传递到我的烧瓶文件并在那里调用另一个函数。我将如何实现这个目标?
JavaScript:
<script>
function ToPython(FreeSearch,LimitContent)
{
alert(FreeSearch);
alert(LimitContent);
}
</script>
Run Code Online (Sandbox Code Playgroud)
我想调用的烧瓶函数:
@app.route('/list')
def alist(FreeSearch,LimitContent):
new = FreeSearch+LimitContent;
return render_template('list.html', title="Projects - " + page_name, new = new)
Run Code Online (Sandbox Code Playgroud)
我想做一些类似于"filename.py".alist(FreeSearch,LimitContent)JavaScript 的事情,但这是不可能的......
我正在使用 cypress 来测试我的 VueJS 应用程序。我遇到的一件事是模拟要显示在页面上的图像。对于我的用例,我只是使用以下代码加载用户配置文件:
describe('Test Login', () => {
it('Can Login', () => {
cy.server();
cy.route({
method: 'GET',
url: '/api/account/',
response: 'fx:profile.json',
});
cy.route('**/media/demo1.png', 'fx:demo1.png');
});
});
Run Code Online (Sandbox Code Playgroud)
fixtures/profile.json
{
"avatar": "http://localhost:8080/media/demo1.png",
"username": "cypress",
"email": "email@cypress.io",
"pk": 1,
"is_staff": true,
"is_superuser": true,
"is_active": true
}
Run Code Online (Sandbox Code Playgroud)
配置文件夹具数据在测试中正确加载。在我的 fixtures 文件夹中,我还有一个demo1.png文件。我希望在测试期间加载此图像并显示在页面上,但它显示为损坏的图像。
在网络选项卡中,它显示demo1.png为带有 200 响应代码和text/html.
cypress 文档主要在上传图像的上下文中讨论图像,但我一直无法找到如何模拟通过<img>标签加载的图像的示例。有没有更简单的方法来做到这一点?
我应该检测 Laravel 5 中浏览器是否支持 Javascript。据我所知,我必须使用 . 但我不确定哪个是我可以在 laravel 5 中使用的第一个 php 文件?
我应该将其包含在 public/index.php 中吗?看起来不正确。期待您的帮助。