简体   繁体   English

XMLHttpRequest 在 function 内部调用时未收到响应

[英]XMLHttpRequest not receiving a response when called inside a function

I have an HTML form that calls upon submitting, the function-我有一个在提交时调用的 HTML 表单,该函数-

function getLocationTemp() {
    temp= new XMLHttpRequest();
    temp.open('GET', 'url', true);
    temp.onload = function () {
        console.log(temp.response);
    }
    temp.send();
}

For some reason, I never receive a response to this API call.出于某种原因,我从未收到对此 API 调用的响应。 I have tried debugging the readyState, and i's always at- 1.我试过调试 readyState,我总是在-1。

Although, when I execute the same block of code outside of the function as such-虽然,当我在 function 之外执行相同的代码块时 -

temp= new XMLHttpRequest();
temp.open('GET', 'url', true);
temp.onload = function () {
    console.log(temp.response);
    }
temp.send();
function getLocationTemp() {
console.log('random');
}

It works, I get the data, and response code as- 4.它有效,我得到数据和响应代码为- 4。

I assumed the issue was that the flow was leaving the function, and so the local onload was out of scope.我认为问题是流程离开了 function,因此本地加载超出了 scope。 So tried running a for loop from 0-100 within the function thinking it would give it enough time to get the response, and also put in a while loop that wouldn't exit until readystate became 4. But the state won't move past 1.所以尝试在 function 中运行一个从 0-100 的 for 循环,认为它会给它足够的时间来获得响应,并且还放入一个在 readystate 变为 4 之前不会退出的 while 循环。但是 state 不会过去1.

What seems to be the issue?似乎是什么问题?

As listed in the above replies, the action is re-rendering the page and killing off the request.如上述回复中所列,该操作是重新渲染页面并终止请求。 This would be rather done as an onclick function handler or a change of the structure to call it using the form tag and its attributes like action and method .这将作为onclick function 处理程序或更改结构以使用表单标记及其属性(如actionmethod )调用它来完成。

An example of doing this via the onclick method would be -通过 onclick 方法执行此操作的示例是 -

function getLocationTemp() {
    temp= new XMLHttpRequest();
    temp.open('GET', 'url', true);
    temp.onload = function () {
        console.log(temp.response);
    }
    temp.send();
}

and in the html something like this -在 html 中是这样的 -

<button  type="button" onclick="getLocationTemp()">Submit Form</button>

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

相关问题 收到 404 响应后重新启动 XMLHttpRequest - Restarting an XMLHttpRequest on receiving a 404 response 接收错误 - 在实现 React Spring 基本示例时,只能在函数组件的主体内部调用 Hooks - Receiving error - Hooks can only be called inside of the body of a function component when implementing React Spring basic example 使用xmlhttprequest响应作为函数 - Using xmlhttprequest response as function XMLHTTPRequest没有收到来自LotusScript代理的响应 - XMLHTTPRequest not receiving response from LotusScript agent 在异步函数中接收有关同步 XMLHttpRequest 的警告 - Receiving warning about Synchronous XMLHttpRequest in asynchronous function 当通过cfajaxproxy从javascript调用时,cfc函数不接收参数 - cfc function not receiving arguments when called from javascript via cfajaxproxy 函数在使用 XMLHttpRequest 从函数调用时按预期工作,但在使用 EventSource 从函数调用时失败。 为什么是这样? - Function works as expected when called from function using XMLHttpRequest, but fails when called from a function using EventSource. Why is this? 在函数内部调用函数时的不同结果 - Different Result when function is called inside a function “ this”是指在私有函数中调用时的窗口 - “this” refers to window when called inside a private function 当在对象内部调用函数时,JavaScript会监听 - JavaScript listen for when a function is called inside an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM