2012-10-01 16 views
8

XMLHttpRequestオブジェクトにパラメータを渡すにはどうすればよいですか?私はjavascriptの代わりにjqueryのを使用する必要がパラメータをXMLHttpRequestオブジェクトに渡す

function setGUID(aGUID) { 

    var xhReq = new XMLHttpRequest(); 

    xhReq.open("POST", "ClientService.svc/REST/SetAGUID" , false); 
    xhReq.send(null); 
    var serverResponse = JSON.parse(xhReq.responseText); 
    alert(serverResponse); 
    return serverResponse; 
} 

は、jQueryのでは私はそれがこのコードで動作するようになったが、ストレートJavaScriptの道をそれを把握するように見えるカント..

function setGUID(aGUID) { 

    var applicationData = null; 

    $.ajax({ 
     type: "POST", 
     url: "ClientService.svc/REST/SetAGUID", 
     contentType: "application/json; charset=utf-8", 
     data: JSON.stringify({ aGUID: aGUID }), 
     dataType: "json", 
     async: false, 
     success: function (msg) { 

      applicationData = msg; 

     }, 
     error: function (xhr, status, error) {); } 
    }); 

    return applicationData; 

} 

答えて

11

たくさんありますインターネット上の "xmlhttprequest post"に関するチュートリアルの私はちょうどそののいずれかをコピーします。

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

https://www.google.com/search?q=xmlhttprequest+post

var http = new XMLHttpRequest(); 
var url = "url"; 
var params = JSON.stringify({ appoverGUID: approverGUID }); 
http.open("POST", url, true); 

http.setRequestHeader("Content-type", "application/json; charset=utf-8"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() { 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(params); 
+0

いただきます。http:

は見てみましょうか?新しいXMLHttpRequest()? –

+0

は固定されていますが、はいです。 – lolol

+2

次の行は、 "安全でないヘッダーエラー"をスローします。 http.setRequestHeader( "Content-length"、params.length); http.setRequestHeader( "Connection"、 "close"); 私はこれらの行なしでこれを動作させようとしていますが、パラメータは設定されていないようです。 – galactikuh