我ExceptionListener在Symfony3中有一个实现(也可以在Symfony2中使用)。在ExceptionListener识别该请求是否是正常的HTTP或AJAX(XmlHttpRequest的),并相应地产生响应。使用jQuery .post()或时.ajax(),ExceptionListener返回$request->isXmlHttpRequest()为TRUE,但是使用javascript时var xhr = new XmlHTTPRequest(),ExceptionListener返回$request->isXmlHttpRequest()为FALSE。我在需要通过AJAX上传文件的少量实例中使用后者(无法使用.post()或来完成).ajax()。
我正在寻找解决方案(前端或后端)以解决我ExceptionListener错误地将其作为普通HTTP请求的问题。
前端代码:
function saveUser()
{
var form = document.getElementById('userForm');
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open('POST', '{{url('saveUser')}}', true);
xhr.onreadystatechange = function (node)
{
if (xhr.readyState === 4)
{
if (xhr.status === 200)
{
var data = JSON.parse(xhr.responseText);
if (typeof(data.error) != 'undefined')
{
$('#processing').modal('hide');
$('#errorMsg').html(data.error);
$('#pageError').modal('show');
}
else …Run Code Online (Sandbox Code Playgroud) 我正在尝试从SharePoint网站及其跨域访问中读取yahoo新闻提要。我正在使用提到的代码进行访问,但是遇到以下错误,我走过许多站点和博客,但仍然没有运气。(我正在Chrome控制台中运行此代码)
未捕获到的SyntaxError:意外令牌<
$.ajax({
type:"GET",
url:"https://www.yahoo.com/news/rss/world",
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
请提出建议!
我想在我的反应代码中使用axios对端口8001上运行的本地快速服务器进行调用,如下所示:
axios.post('http://localhost:8001/enterInfo',{headers: { 'crossDomain': true }}, payload)
.then((response) => {
this.setState({
saved:true
})
})
Run Code Online (Sandbox Code Playgroud)
我的快递服务器正在侦听端口8001.我在快递服务器上写了一个post方法:
app.post('/postUrl', (req, res) => {
console.log(req.body);
res.send('111');
});
Run Code Online (Sandbox Code Playgroud)
但是当我从反应ui进行调用时,它会给出这个错误:
XMLHttpRequest无法加载http:// localhost:8001/enterInfo.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.
所以,我有这个我在2015年写的旧代码,它工作得很好,但现在它不是 - 任何想法是什么错?它在使用ajax时似乎在请求函数中,我收到一个错误:
VM3641:326 Uncaught TypeError: Cannot set property 'response' of undefined
at Object.success (<anonymous>:326:48)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at y (jquery.min.js:4)
at XMLHttpRequest.c (jquery.min.js:4)
Run Code Online (Sandbox Code Playgroud)
错误仍然存在的特定代码段:
if ($canUsejQuery) {
// Make Request with jQuery
console.log('jQuery');
jQuery.ajax({
type: this.RequestData.type,
data: {},
url: this.RequestData.link,
success: function (response) {
console.log(response);
this.ResponseData.response = response;
},
error: function (err, errCode, errMessage) {
this.ResponseData.response = '';
this.ResponseData.hasErrors = true;
console.log('error');
}
});
}Run Code Online (Sandbox Code Playgroud)
这是一个奇怪的,它给出了响应,我尝试将其分配为ResponseData,但它没有做到这一点,并说它未定义,但如果我提醒响应,它工作正常,它不是未定义的?
整个代码:
/**
* @version 1.0.1
* @copyright Copyright …Run Code Online (Sandbox Code Playgroud)我的代码在localhost(垃圾服务器)上运行良好,但是将其移至共享主机后,出现错误403(禁止)。我首先尝试了这个:
$.post('login.php?login', { user_email: user_email, user_password: user_password, user_remember: user_remember }, function(data)
{
// Some code here
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试了这个:
var http = new XMLHttpRequest();
var url = "login.php?login";
var params = "user_email="+user_email+"&user_password="+user_password+"&user_remember="+user_remember;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
// Some code here
}
}
http.send(params);
Run Code Online (Sandbox Code Playgroud)
但我仍然收到此错误:
POST http://domain_name/login.php?login 403(禁止)
知道怎么了吗?
我正在尝试将一个非常小的自定义Web应用程序与NetSuite集成.我希望每当用户点击我的Web应用程序中的按钮时,就可以在NetSuite中创建自定义记录.
我编写了一个与REST API Testing chrome扩展一起使用的RESTlet.我已通过Chrome扩展程序成功创建了记录.
但是,当我尝试从我的Web应用程序POST时,我收到此错误:
"对预检请求的响应未通过访问控制检查:请求的资源上没有'Access-Control-Allow-Origin'标头.因此不允许原点'null'访问.响应具有HTTP状态代码401."
如何使用外部站点的RESTlet向NetSuite发送邮件?我是应该使用RESTlet还是有更好的方法?
我试图弄清楚如何处理HttpHeaders上的标头,以通过HttpClient将它们用于http请求.
const headers = new HttpHeaders();
headers.append('foo', 'bar');
headers.set('foo', 'bar');
console.log(headers.get('foo')) // null
Run Code Online (Sandbox Code Playgroud)
它只能这样工作:
const headers = new HttpHeaders().set('foo', 'bar');
console.log(headers.get('foo')) // bar
Run Code Online (Sandbox Code Playgroud)
有没有一种特殊的方法来添加标题?或者这是一个错误?
这三种调用方法有什么区别?我在我当前的项目中使用fetch,并没有看到它们之间有任何真正的区别.为什么在javascript XD中需要有30种不同的方法来做事情.
谢谢.
我认为互联网上有很多类似的主题,但我只花了 1 小时搜索,仍然无法解决这个问题。
我无法使用 Angular 在我的服务器(Apache 和 PHP)上使用 POST 发出请求。
我在节点 10、apache 2.4 和 php 7.1 中使用 angular/cli v.6.2.1
这是来自 Http 调用的简单代码(HttpClient 和 HttpHeaders 都来自@angular/common/http):
constructor(private http: HttpClient){}
this.http.post('http://localhost/distributor.php', ['prop1':'value1', 'prop2':'value2'], {headers: new HttpHeaders().set('Access-Control-Allow-Origin','*')}).subscribe(
data => {
console.log(data);
},
err => {
console.log('error');
});
Run Code Online (Sandbox Code Playgroud)
}
我只是尝试以这种方式从 PHP 发回一些东西:
<?php
$data = $_POST;
echo json_encode($data);
Run Code Online (Sandbox Code Playgroud)
我已经允许 apache 配置文件中的所有来源。Firefox 和 Chrome 在“选项”预检后都让我失望,并且不做任何其他事情,也没有从 PHP 文件中返回。
这是 FireFox 向我展示的内容:
我可以在网络选项卡中看到这一点:
响应选项卡显示一个完全空的框。
我可以从我的 http.post 中删除自定义标头的部分,它没有任何改变。
对我来说似乎很奇怪的是,我可以单击 FireFox 编辑和重新发送按钮,而无需更改任何内容,然后出现正确的结果...
感谢阅读/帮助
我有一个服务调用的实现,它可能会引发错误,如果出现错误,是否可以重新运行该服务调用?
captureMultiScreen(elem: Alert, overwrite: boolean, success?: Function, failed?: Function) {
this.spinnerValue = true;
const captureModel: Capture = {
member_id: elem.member_id,
gateway: elem.gateway,
account: elem.account,
alert_id: elem.alert_id,
transaction_id: elem.transaction_id,
overwrite: overwrite
};
this.captureService.CaptureScreenService(captureModel).subscribe(result => {
this.capServiceResponse = result;
},
(error) => {
// Retry "CaptureScreenService" here
}
Run Code Online (Sandbox Code Playgroud)
如果到达错误回调,是否有办法再次运行服务调用?
xmlhttprequest ×10
javascript ×6
ajax ×4
angular ×3
cors ×3
jquery ×3
php ×2
angular5 ×1
api ×1
express ×1
fetch ×1
http ×1
http-headers ×1
netsuite ×1
observable ×1
reactjs ×1
rest ×1
restlet ×1
rxjs ×1
suitescript ×1
symfony ×1
typescript ×1