如何使用JS/AJAX/JQUERY为POST FORM添加标题授权? [英] How to add Header Authorization for POST FORM using JS/AJAX/JQUERY?

查看:106
本文介绍了如何使用JS/AJAX/JQUERY为POST FORM添加标题授权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用页面上的表单获取输入并将这些值提交到另一个外部站点上托管的php.请求制作者扩展显示在外部站点上提交数据时,标题授权与其他输入一起传递.

I wanna get inputs using a form on my page and submit those values to a php hosted on another external site. Request maker extension shows the Header Authorization being passed along with other inputs when submitting data on the external site.

结果可能是一个xml文件(学生记录).需要提取并显示为结果.

The result is probably an xml file (Student Record).Need to pull and show it as result.

徒劳地使用$ .Ajax和jquery进行了很多尝试.请帮忙.

Tried a lot using $.Ajax and jquery in vain. Please help.

[1]: http://i.stack.imgur.com/rDO6Z . jpg [2]: http://i.stack.imgur.com/92uTh . jpg

[1]: http://i.stack.imgur.com/rDO6Z. jpg [2]: http://i.stack.imgur.com/92uTh. jpg

function myFunction() {
var name = document.getElementById("name").value;

// AJAX code to submit form.
$.ajax({
type: "POST",
url: "http://externalsite.cpm/results.php.php",
data: dataString,
  beforeSend: function(xhr) {
	
    xhr.setRequestHeader('Authorization', "Basic XXXXXXXXXXXXXXXXXXXXXXXXX" );   or xhr.setRequestHeader('Authorization', "Basic " +btoa(ser_user + ':' + ser_pass));
						},
cache: false,
success: ???


xmlhttp.open("POST","your_url.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=" + name + "&email=" + email);

<body>

<form action="http://externalsite.cpm/results.php" method="post" >
	Enter Your Name<br>
	<input type="text" name="name" >
	<br>
	<input type="submit" onclick="myFunction()" value="Submit">
	
</body>

在将页面中的值提交到外部php时,如何添加此标头授权?请帮忙!

How do I add this header authorization when submitting values from my page to external php? Please Help !

推荐答案

虽然有点晚,但仍为将来可能遇到相同问题的读者提供了答案. 如果在ajax请求中明确提及表单,请不要在html代码中提供表单提交URL(操作)和方法类型(POST). 请注意,在给定的代码片段中添加了相应的更改.

Its bit late but still posting the answer for future readers who might face the same issue. Please do not provide the form submit url (action) and method type (POST) inside the html code when it is explicitly mentioned inside the ajax request. Notice the corresponding changes added in the code snippets given.

HTML表单代码:

            <form><!---Removed form submit url-->
            <input type="text" id="keyName" value="testValue">
            <!---Id attribute added to input field to be submitted--->
            <input type="button" onclick="myFunction()" value="Submit">
            <!---Input type is button NOT submit--->
            </form>

JavaScript代码:

function myFunction(){
var dataValue = $("#keyName").val();
$.ajax({
            type : 'POST',
            //remove the .php from results.php.php
            url : "http://externalsite.cpm/results.php",
            //Add the request header
            headers : {
                Authorization : 'Bearer ' + 'XXXXXXXXXXXXXXXXXXXXXXXXX'
            },
            contentType : 'application/x-www-form-urlencoded',
            //Add form data
            data : {keyName : dataValue},
            success : function(response) {
                console.log(response);
            },
            error : function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                console.log(err);                   
            }
        }); //End of Ajax
}   // End of myFucntion

更新:

PHP服务results.php

<?php 
     print_r($_POST["keyName"]);
?>

这篇关于如何使用JS/AJAX/JQUERY为POST FORM添加标题授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆