473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I write a common XMLhttpRequest function for both GET and POST?

I wrote XMLhttpRequest function for making a ajax POST.When I add new job this function is called and added job is also shown in HTML.The code is below.
Expand|Select|Wrap|Line Numbers
  1. function req_add()
  2.         {
  3.             var hr = new XMLHttpRequest();
  4.             var url = "To-Do.php";
  5.             var content = document.getElementById("content").value;
  6.             var vars = "content=" + content;
  7.  
  8.             hr.open("POST", url, true);
  9.             hr.setRequestHeader("Content-type","application/x-www-form-
  10. urlencoded");
  11.             hr.onreadystatechange=function()
  12.             {
  13.                 if(hr.readyState == 4 && hr.status == 200)
  14.                 {
  15.                     var return_data = hr.responseText;
  16.                     document.getElementById("result").innerHTML               
  17. = return_data;
  18.                 }
  19.             }
  20.             hr.send(vars);
  21.             document.getElementById("result").innerHTML =   
  22.  
  23. "Processing...";
  24.         }
In advance I was using $.getJSON for GET operation.Now I want to write a function that both GET and POST requests can be done.The function will be like this=> makeRequest(type,params,URL) ,type is for POST and GET. There will be onsuccess function whether the data is returned successfully or not.And when I write common function will I use hr.send ()? Thanks.
Mar 12 '13 #1
3 1617
johny10151981
1,059 1GB
use jquery, it will make your life simple.

Expand|Select|Wrap|Line Numbers
  1. function makeRequest(type, path, params, callback)
  2. {
  3. $.ajax({url: path, type: type, dataType: 'html', data: parameter, timeout: 90000, success: callback(returned_text)});
  4. }
  5.  
Mar 13 '13 #2
This is another way.But my instructor does not want like this.
I tried the below code, but it didn't work.Can you fix it?
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>To-Do</title>
  4.         <meta name="description" content="To-Do" charset="utf-8"></meta>
  5.         <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js" ></script>
  6.         <script type="text/javascript" src="jquery.js"></script>
  7.         <script language="javascript" type="text/javascript"> 
  8. function mr(type,params,URL){
  9.  
  10.             var hr = new XMLHttpRequest();
  11.             alert("mr");
  12.                 if(type=='POST')
  13.                 {
  14.                 alert("Post");
  15.                 var content = document.getElementById("content").value;
  16.                 var vars = "content=" + params;
  17.                 hr.send(vars);
  18.                 }
  19.                 if(type=='GET')
  20.                 {
  21.                 alert("Get");
  22.                 hr.send();
  23.  
  24.                 }
  25.                 document.getElementById("result").innerHTML = "Processing...";
  26.  
  27.                 hr.open(type, URL, true);
  28.                 hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  29.  
  30.                 hr.onreadystatechange=function()
  31.                 {
  32.                     if(hr.readyState == 4 && hr.status == 200)
  33.                     {
  34.                         var return_data = hr.responseText;
  35.                         document.getElementById("result").innerHTML = return_data;
  36.                     }
  37.                     else{
  38.  
  39.                         alert("Not successful request!");
  40.                     }
  41.                 }
  42.  
  43.  
  44.  
  45.     }
  46.  
  47.  
  48.         </script>
  49.         <style type="text/css">
  50.         button { cursor: pointer }
  51.     div {
  52.         color:#666;
  53.         font: normal 13px "Trebuchet MS";
  54.         width: 350px;
  55.         padding: 10px
  56.     }
  57. </style>
  58.     </head>
  59.  
  60.     <body>
  61.           Add Item: <input type="text" name="name" id="content"><br>
  62.           <button onclick="javascript:mr('POST','To-Do.php');" type="button" id="btn1">Submit</button><br>
  63.           <button onclick="javascript:mr('GET','Jobs.json');" type="button" id="btn2" >List Jobs</button>
  64.         <div id="result"> </div>
  65.     </body>
  66. </html>
Mar 13 '13 #3
acoder
16,027 Expert Mod 8TB
For GET, you need to add the params to the URL.
Mar 13 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Henri Schomäcker | last post by:
Hi folks, I have a quite big class, which I want to use on UNIX-like systems and on win32. Until now, everything is absolutely portable. But now I need to read a directory and use the os...
6
by: tings | last post by:
How to write such a function that can take varible number and tyoes of arguments, like printf("... %d, %s...", myInt, myString...)? Thanks for for your help!
3
by: Wen | last post by:
hello, now, i wanna port a c++ program into C# project. a DLL written by C++ and keep it no change, and UI wirtten by C#. my C++ UI code is as below: // error handlers --global function...
3
by: datttanand | last post by:
How to write the main function in java script? such as in vb script sub main end sub
1
by: rasmidas | last post by:
I have a function sEntityFree() which is a function from a third party S/w we are using. For this we have our enhanced code. sEntityFree() has been called inside our enhanced code 2000 or 3000...
0
by: nabil035 | last post by:
I explain exactly what I want to do: write a callback function in a C++/CLI application this application imports function from a native DLL call this function from the DLL and return the...
7
by: mynkow | last post by:
Hi, I want to write an universal function called isNegative( param ) where the param can be float, int, double, long etc. The result will be true/false. Any comments?
13
by: Kong Chun Ho | last post by:
Hi, i have created a XMLHttpRequest function, but it didn't work, this is my code function CheckAccount (acc) { CreateXMLHttpRequest(); if (ready == 1) { if (!xmlHttp) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.