Ajax请求未传递给控制器(Ajax request not passed to controller)
我向控制器发送了一个AJAX请求。 这个开发在JSP和Spring环境中完成。 SimpleFormController被我正在使用的控制器覆盖。 使用JavaScript我创建对象并发送请求。 此请求未传递给控制器。 如果您对此问题有任何疑问,请帮助我。
发送请求的JavaScript代码。
function getStates(){ var httpRequest; var country = document.getElementById('countryName'); alert(country); var url = '/developer/register.htm'; url = url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true'); if (window.ActiveXObject) { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } httpRequest.open("GET", url, true); httpRequest.onreadystatechange = function() {processRequest(); }; httpRequest.send(null); } function processRequest() { if(httpRequest.readyState == 4){ alert("inside ready state"); var response = http.responseText; document.getElementById('states').innerHTML = response; } }
变量
url
以我在调度程序servlet中提到的方式给出。浏览器在processRequest()函数中显示错误 ,告知未定义httpRequest。 但是,我在前面的行中没有得到这个错误 ,我在getStates()函数中使用了这个对象。 如果有人对此有所了解,请帮我解决这个问题。 在此先感谢并感谢您的支持。
I send an AJAX request to my controller. This development done in JSP and Spring environment. SimpleFormController is overridden by the controller I'm using.
Using JavaScript I create the object and send the request. this request is not getting passed to controller.
JavaScript code which send the request.
function getStates(){ var httpRequest; var country = document.getElementById('countryName'); alert(country); var url = '/developer/register.htm'; url = url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true'); if (window.ActiveXObject) { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } httpRequest.open("GET", url, true); httpRequest.onreadystatechange = function() {processRequest(); }; httpRequest.send(null); } function processRequest() { if(httpRequest.readyState == 4){ alert("inside ready state"); var response = http.responseText; document.getElementById('states').innerHTML = response; } }
Variable
url
is given in the way how I have mentioned in dispatcher servlet.The browser shows an error in processRequest() function telling that httpRequest is not defined. but, I don't get this error in the previous lines. I use this object in getStates() function.
原文:https://stackoverflow.com/questions/8118690
最满意答案
我认为错误在这里:
while(ptr->next != NULL){ ptr = ptr->next; count++; }
假设你的链表中有2个元素。 然后
while
循环只迭代一次,count
将为1.当你进入for
循环时,它也只会迭代一次,这意味着你将正确地重新分配新头的指针,而不是第二个元素(以前的头脑)。如果将
count
初始化为1而不是0,则应该正确反映链表中元素的数量,并且for
循环应该正确执行。编辑:您还必须稍微重构
for
循环以避免列表末尾的段错误:Node* temp; for (int i = 0; i < count; i++) { temp = ptr->next; ptr->next = ptr->prev; ptr->prev = temp; ptr = ptr->next; }
I think the error is here:
while(ptr->next != NULL){ ptr = ptr->next; count++; }
Let's say your linked list has 2 elements in it. Then that
while
loop will only iterate once, andcount
will be 1. When you get down to thefor
loop, it will also only iterate once, which means you will correctly reassign the pointers for the new head, but not the second element (previously the head).If you initialize
count
to 1 instead of 0, it should correctly reflect the number of elements in the linked list and thefor
loop should execute correctly.Edit: You will also have to restructure your
for
loop slightly to avoid a segfault at the end of the list:Node* temp; for (int i = 0; i < count; i++) { temp = ptr->next; ptr->next = ptr->prev; ptr->prev = temp; ptr = ptr->next; }
相关问答
更多-
您正在从tailInsert返回temp(最后一个节点)并分配给head。 如果要插入尾部,请不要更改头指针。 void tailInsert(node *head, int data) { if(head->next == NULL) { node *temp; temp = createNode(data); temp->next = NULL; temp->prev = head; head->next = temp; ...
-
我认为错误在这里: while(ptr->next != NULL){ ptr = ptr->next; count++; } 假设你的链表中有2个元素。 然后while循环只迭代一次, count将为1.当你进入for循环时,它也只会迭代一次,这意味着你将正确地重新分配新头的指针,而不是第二个元素(以前的头脑)。 如果将count初始化为1而不是0,则应该正确反映链表中元素的数量,并且for循环应该正确执行。 编辑:您还必须稍微重构for循环以避免列表末尾的段错误: Node* temp ...
-
双重链接列表实施(Doubly Linked Lists Implementation)[2022-02-09]
它与单链表的不同之处在于节点可以插入任何地方,不仅仅是在头部之后或尾部之后使用可用的下一个和上一个节点,而在单链表中,这个插入列表中的任何地方都是不可能的? 这是错的。 您可以在单个链接列表的中间插入节点就好了。 唯一的区别是双链表更容易导航,因为您可以从任何给定节点向前和向后移动它。 如果想要在双向链表中插入节点,那么默认参数应该是待插入节点之后的节点或待插入节点之前的节点? 这完全取决于您使用列表的内容。 链接列表由于其随机访问性能差而不是真正的通用数据结构。 因此,它们通常仅用于迭代遍历列表的算法,并 ... -
private void DeleteItemHelper(final int indexToDelete, int curIndex, DLLNode curNode) { if (curIndex == indexToDelete) { // Handle removing a node with both a previous and next nodes. } else { DeleteItemHelper(indexToDelete, cu ...
-
关于双重链表(about doubly linked list)[2023-08-10]
取决于链表的具体实现。 两种方式都可以工作,但有额外的“头/尾”元素大大简化了插入/删除等许多操作的实现(因为你总是可以确定会有下一个/前一个元素,节省了大量的条件代码处理列表的开始/结束)。 由于您将问题标记为Java,我建议您查看java.util.LinkedList的JDK源代码。 您将发现在此特定实现中,head和tail不引用元素。 如果你真的想要理解额外的头/尾节点的优势,如果你自己实现一个简单的双链表来亲自体验插入/删除方法的复杂性差异,那么它可能是最有启发性的。 在允许指针算术的其他语言中 ... -
您的代码没有设置足够的指针。 通常,您必须确保新节点的prev是正确的; 它是next是正确的; 前一个条目的next是正确的; 并且下一个条目的prev是正确的(如果前一个或下一个条目为NULL,则要小心)。 假设全局变量是: value *entry_head = NULL; value *entry_tail = NULL; 然后插入新value *ent (已经用值初始化)的代码是: if (entry_head == NULL) { assert(entry_tail == NULL); ...
-
扭转双向链接列表(Reversing a Doubly Linked List)[2021-11-08]
让我们一次尝试一下几行代码。 Node temp=head; head=tail; tail=temp; 这里我们只是设置一些变量。 我们正在交换头部,指向尾部和尾部。 现在我们定义我们的起始节点。 这是我们的新头,曾经是尾巴。 Node p=head; //create a node and point to head while(p!=null) { temp=p.next; 在这一点上,这就是我们所看到的(注意:如果这是第一次迭代, next将指向null,但这并不重要,只是假设A在 ... -
在代码中,newTail总是一个接一个。 在while循环中,newTail-> next设置为iter,iter-> prev设置为newTail。 哪个没效果。 也许这个图表会有所帮助 尝试这个。 它循环遍历列表,并为每个节点交换next和prev指针。 (这可能不是最有效的算法。) Node* Reverse2(Node* head) { if (head == nullptr) {return head;} Node *iter = head; Node *tail = nullptr ...
-
把链表的头部移到尾部(Moving head of linked list to tail)[2022-08-19]
你想删除列表的头部并使其成为新的尾部。 你应该弄清楚如何在脑海中做到这一点,代码将是一个逻辑表示。 删除列表的头部。 新头成为下一个项目。 现在,被删除的物品是独立的; 它之后什么也没有。 将节点放在列表的末尾。 新尾部成为该节点。 正如您所看到的,您现在的代码并不完全如此。 一次完成一个步骤: 那么,第1步: Node node = head; head = head.next; // <- remove head, new head becomes next item 然后,第2步: node.nex ... -
双重链表(Doubly linked lists)[2022-03-24]
if(head == null) { head = new CardInfoNode(); head.setCardInfo(info); head.setNext(tail); tail.setPrev(node) // here lies the problem. tail must be set to something // to make it doubly-lin ...