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. -
function req_add()
-
{
-
var hr = new XMLHttpRequest();
-
var url = "To-Do.php";
-
var content = document.getElementById("content").value;
-
var vars = "content=" + content;
-
-
hr.open("POST", url, true);
-
hr.setRequestHeader("Content-type","application/x-www-form-
-
urlencoded");
-
hr.onreadystatechange=function()
-
{
-
if(hr.readyState == 4 && hr.status == 200)
-
{
-
var return_data = hr.responseText;
-
document.getElementById("result").innerHTML
-
= return_data;
-
}
-
}
-
hr.send(vars);
-
document.getElementById("result").innerHTML =
-
-
"Processing...";
-
}
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.
3 1617
use jquery, it will make your life simple. -
function makeRequest(type, path, params, callback)
-
{
-
$.ajax({url: path, type: type, dataType: 'html', data: parameter, timeout: 90000, success: callback(returned_text)});
-
}
-
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? - <html>
-
<head>
-
<title>To-Do</title>
-
<meta name="description" content="To-Do" charset="utf-8"></meta>
-
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js" ></script>
-
<script type="text/javascript" src="jquery.js"></script>
-
<script language="javascript" type="text/javascript">
-
function mr(type,params,URL){
-
-
var hr = new XMLHttpRequest();
-
alert("mr");
-
if(type=='POST')
-
{
-
alert("Post");
-
var content = document.getElementById("content").value;
-
var vars = "content=" + params;
-
hr.send(vars);
-
}
-
if(type=='GET')
-
{
-
alert("Get");
-
hr.send();
-
-
}
-
document.getElementById("result").innerHTML = "Processing...";
-
-
hr.open(type, URL, true);
-
hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
-
-
hr.onreadystatechange=function()
-
{
-
if(hr.readyState == 4 && hr.status == 200)
-
{
-
var return_data = hr.responseText;
-
document.getElementById("result").innerHTML = return_data;
-
}
-
else{
-
-
alert("Not successful request!");
-
}
-
}
-
-
-
-
}
-
-
-
</script>
-
<style type="text/css">
-
button { cursor: pointer }
-
div {
-
color:#666;
-
font: normal 13px "Trebuchet MS";
-
width: 350px;
-
padding: 10px
-
}
-
</style>
-
</head>
-
-
<body>
-
Add Item: <input type="text" name="name" id="content"><br>
-
<button onclick="javascript:mr('POST','To-Do.php');" type="button" id="btn1">Submit</button><br>
-
<button onclick="javascript:mr('GET','Jobs.json');" type="button" id="btn2" >List Jobs</button>
-
<div id="result"> </div>
-
</body>
-
</html>
For GET, you need to add the params to the URL.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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!
|
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...
|
by: datttanand |
last post by:
How to write the main function in java script?
such as in vb script
sub main
end sub
|
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...
|
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...
|
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?
|
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) {
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
| |