jQuery AJAX:所请求的资源上不存在"Access

编程入门 行业动态 更新时间:2024-10-28 00:28:02
本文介绍了jQuery AJAX:所请求的资源上不存在"Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从localhost:4502端口将数据发布到API.当我尝试使用POSTMAN将数据发布到此API时,通过提供基本授权密钥将数据添加到了后端中.我正在尝试在Ajax Jquery调用中实现的功能相同,但是却收到了CORS错误.我第一次尝试在jquery中发布数据,请在这里帮助我可以添加的内容.我已经获得了用于基本授权的API密钥,因为用户名和密码可以保留为空白.

I am trying to post data to an API from my localhost:4502 port. When i tried to post data to this API using POSTMAN the data got added in the backend by providing the Basic Authorization key. The same i am trying to implement here in the Ajax Jquery call, but getting an CORS error. First time in jquery i am trying to post the data, please help here, what i can add. I have got the API key to for the Basic Authorization as a Username and Password can be left blank.

<script src="ajax.googleapis/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#Save").click(function(){ var person = new Object(); person.Name = $('#Name').val(); person.EmailAddress = $('#EmailAddress').val(); person.CustomFields = [0]; person.CustomFields[0].Key = "[Country]"; person.CustomFields[0].Value = $('#Country').val();; $.ajax({ url: 'api.createsend/api/v3.1/subscribers/7c7a6087b0e450ad72b38be83098e271.json', type: 'POST', dataType: 'json', data:person, success: function(data,textStatus,xhr){ console.log(data); }, error: function(xhr,textStatus,errorThrown){ console.log('Error Something'); }, beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic OTdlMjVmNWJiMTdjNzI2MzVjOGU3NjlhOTI3ZTA3M2Q5MWZmMTA3ZDM2YTZkOWE5Og=="); } }); }); }); </script>

推荐答案

我添加了dataType:'jsonp',并且有效!

I have added dataType: 'jsonp' and it works!

$.ajax({ type: 'POST', crossDomain: true, dataType: 'jsonp', url: '', success: function(jsondata){ } })

更多推荐

jQuery AJAX:所请求的资源上不存在"Access

本文发布于:2023-10-12 01:03:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上不   资源   AJAX   jQuery   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!