为什么XMLhttprequest函数在使用Restful Api时无法发布?

时间:2013-03-19 19:07:06

标签: php javascript xmlhttprequest restful-architecture

My html code is below.
<html>
<head>
<title>To-Do</title>
<meta name="description" content="To-Do" charset="utf-8"></meta>
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
<link href="css/doc.css" rel="stylesheet" type="text/css" />
<script src="js/bootstrap.js"></script>
<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,content,URL) {

            var hr = new XMLHttpRequest();


            //var content = document.getElementById("content").value;
            var vars = "content=" +content.value;

            //if (type == 'GET')
                //URL = URL + '?' + vars;



            hr.onreadystatechange = function() {
                if (hr.readyState == 4 && hr.status == 200) {

                    var return_data = hr.responseText;
                    document.getElementById("result").innerHTML = return_data;

                }

                }

           hr.open(type, URL, true);
           hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            switch(type) {
                case 'GET':
                    hr.send();
                    break;
                case 'POST':
                     hr.send(vars);
                    break;
            }

           document.getElementById("result").innerHTML = "Processing...";

        }
</script>
</head>
<body>
<div align="center">
    <h1>To-Do Test</h1>
    <strong>Add Item</strong>
    <div class="input-append">
        <input type="text" name="name" id="content" height="50px" >
        <button onclick="javascript:mr('POST',content,'ToDo.php')" class="btn"><i         
 class="icon-ok"></i>Add</button>
    </div></br>
    <button class="btn" type="button"    
 onclick="javascript:mr('GET',content,'ToDo.php');" id="btn2">List Jobs</button>
</div>
<div id="result"> </div>
</body>

使用此代码,我想使用XMLhttprequest函数作为Ajax GET和POST请求。我尝试简单的get.php和post.php它的工作原理.XMLhttprequest函数似乎是真的。 但是有了restful api只能获得请求工作,发布请求不起作用。为什么这样我无法理解。 项目的其余部分位于https://drive.google.com/#folders/0B1RbKsJ4B8eoeFE5YTY1aFJJd28

0 个答案:

没有答案