javascript中的Ajax post方法插入数据 [英] Ajax post method in javascript to insert data
问题描述
大家好,
我在javascript上运行,通过POST方法从用户输入插入数据..在SQL数据库中
javascript post方法如下:
Hello everyone,
I have on javascript which is functioning to insert data from user input through "POST" method..in SQL database
the javascript post method is as follows :
$.ajax({
type: "POST",
url: "ajax_functions.aspx?action=insert_question&answers="+encodeURIComponent(answers)+"&question="+encodeURIComponent(true_false_question)+"&correctanswer="+correct_answer+"&type=true_false",
success: function (data) { alert('Question inserted successfully !!'); },
error: function (XMLHttpRequest, exception) {
if (XMLHttpRequest.status == 404) { alert('404: Requested page not found.\nPlease try to submit again.'); }
else if (XMLHttpRequest.status == 500) { alert('500: Internal Server Error.\nPlease try to submit again.'); }
else if (exception === 'parsererror') { alert('Parse error has been occurred.\nPlease try to submit again.'); }
else if (exception === 'timeout') { alert("Server detected connection problem.\nPlease try to submit again."); }
else if (exception === 'abort') { alert('Asynchronous request aborted.\nPlease try to submit again.'); }
else if (XMLHttpRequest.status === 0) { alert('Network connection failed.\nPlease try to submit again.'); }
else { alert('Uncaught Exception.\nPlease try to submit again.'); }
}
});
它通过查询字符串插入数据,但问题是它不适用于大数据。 。我有非常大量的HTML格式的数据..(输入是ckeditor)并且要插入,只能通过javascript ...请帮助我如何实现这一点。
提前感谢你们...
Krunal
It inserts the data through a query string , but the problem is that it doesnt works with large data...I have very huge amount of data which is in HTML format..(the input is ckeditor)and is to be inserted and that is only through javascript only... please help me how to achieve this.
Thanking you all in advance...
Krunal
推荐答案
.ajax({
类型: POST,
url: ajax_functions.aspx?action = insert_question& answers = + encodeURIComponent(answers)+ & question = + encodeURIComponent(true_false_question)+ & correctanswer = + correct_answer + & type = true_false,
成功:函数(数据){alert(' 问题插入成功!!'); },
错误:function(XMLHttpRequest,exception){
if (XMLHttpRequest.status == 404 ){alert(' 404:找不到请求的页面。\\ n请尝试再次提交。 跨度>); }
else if (XMLHttpRequest.status == 500 ){alert(' 500:内部服务器错误。\\ n请尝试再次提交。) 跨度>); }
else if (例外=== ' parsererror'){alert(' 已经发生了解析错误。\\ n请尝试再次提交。); }
else if (例外=== ' timeout'){alert( 服务器检测到连接问题。\\ n请尝试再次提交。); }
else if (例外=== ' abort'){alert(' 异步请求已中止。\\ n请尝试再次提交。); }
else if (XMLHttpRequest.status === 0 ){alert(' 网络连接失败。\\ n请尝试再次提交。'< /跨度>); }
else {alert(' Uncaught Exception 。\ n请尝试再次提交。'); }
}
});
.ajax({ type: "POST", url: "ajax_functions.aspx?action=insert_question&answers="+encodeURIComponent(answers)+"&question="+encodeURIComponent(true_false_question)+"&correctanswer="+correct_answer+"&type=true_false", success: function (data) { alert('Question inserted successfully !!'); }, error: function (XMLHttpRequest, exception) { if (XMLHttpRequest.status == 404) { alert('404: Requested page not found.\nPlease try to submit again.'); } else if (XMLHttpRequest.status == 500) { alert('500: Internal Server Error.\nPlease try to submit again.'); } else if (exception === 'parsererror') { alert('Parse error has been occurred.\nPlease try to submit again.'); } else if (exception === 'timeout') { alert("Server detected connection problem.\nPlease try to submit again."); } else if (exception === 'abort') { alert('Asynchronous request aborted.\nPlease try to submit again.'); } else if (XMLHttpRequest.status === 0) { alert('Network connection failed.\nPlease try to submit again.'); } else { alert('Uncaught Exception.\nPlease try to submit again.'); } } });
它通过查询字符串插入数据,但问题是它不适用于大数据。 。我有非常大量的HTML格式的数据..(输入是ckeditor)并且要插入,只能通过javascript ...请帮助我如何实现这一点。
提前感谢大家...
Krunal
It inserts the data through a query string , but the problem is that it doesnt works with large data...I have very huge amount of data which is in HTML format..(the input is ckeditor)and is to be inserted and that is only through javascript only... please help me how to achieve this.
Thanking you all in advance...
Krunal
而不是在查询字符串中发送数据,这是有限制的根据您的了解,将'Data:'属性添加到.ajax调用:
Instead of sending data in querystrings, which there is a limit to, as you have learned, add the 'Data:' attribute to .ajax call:
>
.ajax({
type: POST,
url: ajax_functions.aspx / WebMethod1,
data: {action:'insert_questions& answers',question:' + encodeURICo mponent(true_false_question)+ '},
成功:...,
错误:...
.ajax({ type: "POST", url: "ajax_functions.aspx/WebMethod1", data: "{ action: 'insert_questions&answers', question: '" +encodeURIComponent(true_false_question)+ "' }", success:..., error:...
注意:{}括号表示它是json。
注2:url:ajax_functions.aspx / webmethod1,将web方法添加到该页面以处理ajax调用和数据。
注3:Web方法签名必须完全匹配,包括'数据的大小写: 'items。
eg
Note : the "{ }" parenthesis mean it's json.
Note2: url: "ajax_functions.aspx/webmethod1", add the web method to that page to handle the ajax call and data.
Note3: the web method signature must match exactly including case for 'data:' items.
e.g.
[WebMethod]
// Let .net handle the request and response as json automatically.
public void WebMethod1(string action, string question){
// Add specific error handling code here, don't confuse this with the error: item of the ajax call.
}
这篇关于javascript中的Ajax post方法插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!