开发者

How to run run a post url?

开发者 https://www.devze.com 2023-03-05 02:19 出处:网络
What i am trying to do is basically run a script and post data to a page but the page is not on the server. So i dont want to run a redirect, just run a webpage in the back when the user clicks on a b

What i am trying to do is basically run a script and post data to a page but the page is not on the server. So i dont want to run a redirect, just run a webpage in the back when the user clicks on a button?

I have tried...

    set httpRequest = CreateObject("WinHttp.WinHttprequest.5.1")
Dim var1

var1 = Request("username")

on error resume next
httpRequest.Open "POST", "http://www.example.com", True
httpRequest.setRequestHe开发者_JAVA技巧ader "Content-Type", "application/x-www-form-urlencoded"
httpRequest.send var1
set httpRequest = nothing

But this doesnt seem to work. So i want to built the url http://www.example.com?username and run it?


The easiest way is to include a reference to the jQuery script libraries and use .ajax

http://api.jquery.com/jQuery.ajax/

a call is simply:

<html>
 <head>
 <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" ></script>
 </head>
<body>
bip
<div id="result"> results to get replaced here</div>
<div id="msg" style="color:Red">any errors will show here</div>
<input type="button" value="click me" id="loadButton" />

<script language="javascript" type="text/javascript">

        //ensure jQuery is loaded before calling script
    $(document).ready(function () {
        alert('ready');
        $('#loadButton').click(function () {
            alert('Handler for .click() called.');
            $.ajax({
                url: 'yoursite/yourpage',
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('doh an error occured. look at the error div above : )');
                    $('#msg').html(xhr.status + ' ' + thrownError); ;
                },
                data: "{'someparam':'someparamvalue','someparam2':'someparamvalue2'}",
                success: function (data) {
                    $('#result').html(data);
                    alert('Load was performed.');

                }
            });
        });
    });

</script>

</body>
</html>


Your problem is most likely this line:

httpRequest.Open "POST", "http://www.example.com", True

Your "True" there is saying run in asynchronous mode, i.e. don't wait for the response before carrying on. You are then destroying the object immediately so the request is never going to get very far. Change that "True" to a "False" and you should see the result hit the other server.


Edit 1:

Also noticed you are not formatting the POST data correctly, it should take the traditional url formatted foo=bar, so the send line needs to be modified like so:

httpRequest.send "name=" & var1

Sorry I didn't spot this first time!


Edit 2:

Here is an example of a working GET transaction with WinHttpRequest:

Function GetHTTP(strURL)

    Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
    objWinHttp.Open "GET", strURL, False
    objWinHttp.Send

    GetHTTP = objWinHttp.ResponseText

    Set objWinHttp = Nothing

End Function

If you do really just need a GET transaction, you can use the following with this function:

strResponse = GetHTTP("http://www.example.com/?name=" & Request("username"))

And as you don't need the response, just ignore strResponse from there on in.

Reference:

NeilStuff.com - Open Method

0

精彩评论

暂无评论...
验证码 换一张
取 消