Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

adding header to posting function in JQuery?

Dear Experts,
I'm new to JQuery, I'd like to post some data to a page but I need to add header information.
How can I add this header to below code?

headers: ['Content-Type: application/json','Authorization: Autho xxxxxxx'],

thank you

	<script type="text/javascript">
        $(document).ready(function () {
			
		 $('#ajaxBtn').click(function(){
			 
   		
			$.post('theURLtoPOST',   // url
			    {"ProductId":"1","Id":"54"},

			   function(data, status, jqXHR) {// success callback
						$('p').append('status: ' + status + ', data: ' + data);
				});
			});
    });
    </script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Dear leakim971, thank you so much.
it is perfect, however, I need to write the hedader information just as it is

headers: ['Content-Type: application/json','Authorization: Autho xxxxxxxxxxxxxxxxxxxxxxxxxx'],
and it doesn't seems to work.

what should i do?

Normaly with php, I post it like this

$headers = ['Content-Type: application/json','Authorization: Autho xxxxxxxxxxxxxxxxx'];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
what about :
headers: '["Content-Type":"application/json","Authorization":"Autho xxxxxxx"]'
Avatar of BR

ASKER

Dear leakim971,
thank you so much for your help,
it posts the data but the header is not working. I do not know why?

	<script type="text/javascript">

$(document).ready(function () {
	$.ajaxSetup({
	    
				
		headers: {'Content-Type': 'application/json','Authorization': 'Bearer TOKEN' }
	});
	
	$('#ajaxBtn').click(function() {
		$.post('https://......', {"ProductId":"1","VirId":"54"}, function(data, status, jqXHR) {// success callback
			$('p').append('status: ' + status + ', data: ' + data);
		});
	});
});

    </script>

Open in new window

Avatar of BR

ASKER

Dear leakim971,
thank you

I think this problem is “No 'Access-Control-Allow-Origin'
I will check this
thank you