Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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 :

C#
$.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.'); }
     }
});

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
Posted
Comments
joginder-banger 6-Feb-14 10:17am    
Gud question, I give 5

Instead of sending data in querystrings, which there is a limit to, as you have learned, add the 'Data:' attribute to .ajax call:

JavaScript
$.ajax({
     type: "POST",
     url: "ajax_functions.aspx/WebMethod1",
     data: "{ action: 'insert_questions&answers', question: '" +encodeURIComponent(true_false_question)+ "' }",
     success:...,
     error:...


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.

C#
[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.
}
 
Share this answer
 
use the data tag in your ajax as said by onenomi.. and to pass in this data tag create a JSON String to send in it..


like if i could give example with your code. YOu can create a JSON String like this.


C#
var params = JSON.stringify({ val1: encodeURIComponent(answers), val2: encodeURIComponent(true_false_question)});


and than pass it in data tag.

For further help you can look on my another answer as well HERE..

Hope it will help you... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900