0

I need to do a getJSON request, but how do i pass authorization and custom headers? The authorization value is asp.net sessionID(ASP.NET_SessionId:drfgjsdfkgdff4534) saved in cookie.txt and custom header is user agent(MozillaXYZ/1.0).

Could any one show me how i can send sessionID value and custom header via getjson request ?

in php we send sessionId from cookie and custom header useragent like this:

curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"

But how to send these 3 header items using getjson ?

 <script>
$.getJSON('http://anyorigin.com/get?url=http://www.someremotesite.com/page3.php&callback=?', function(data){
    //$('#output').html(data.contents);

 var siteContents = data.contents;    

//writes to textarea 
document.myform.outputtext.value = siteContents ;

</script>

edited: I used ajax instead of getjson but after using beforeSend the ajax stopped pulling data!could any one tell me what is wrong?

<html>
<head>

  <script src="http://anyorigin.com/jquery-1.4.2.min.js"></script>

<script>

$.ajax({
        url: 'http://anyorigin.com/get?url=http://www.someremotesite.com/page3.php&callback=?',
        type: 'GET',

        dataType: "json",
        success: displayAll
        beforeSend: setHeader
    });



function displayAll(data){
   // alert(data.contents);

document.myform2.outputtext2.value = data.contents ;
}



function setHeader(xhr) {


        xhr.setRequestHeader('ASP.NET_SessionId', 'rtretretertret43545435454');

      }


</script>
</head>

<body>
<br>



<form id="myform2" name="myform2" action="./3.php?Id=&title=test" method="post">
<td><textarea rows="14" cols="15" name="outputtext2" style="width: 99%;"></textarea></td>

</form>


</html>
4

1 回答 1

1

试试这个来设置标题:

$.ajax({
    url: 'http://something.com/ajax',
    type: 'GET',
    dataType: "json",
    success: displayAll,
    headers : {
        'ASP.NET_SessionId': 'rtretretertret43545435454'
    }
});

不要忘记在“displayAll”之后添加逗号,因为它在您的问题中缺失。

于 2013-06-14T19:28:24.293 回答