跨域ajax功能 [英] cross domain ajax function

查看:106
本文介绍了跨域ajax功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让ajax函数从url请求数据,但脚本会抛出错误警报。在控制台日志窗口中,它显示以下消息:



跨源请求被阻止:同源策略不允许读取远程资源http://test.domain.com/test2.aspx。这可以通过将资源移动到同一个域或启用CORS来解决。



我在web.config页面中添加了以下标题,但是当我从主机(test.domain.com/test2.aspx)请求时,我在控制台日志中收到以下消息,其中包含404响应 - 未找到:


test.domain.com/test2.aspx中的
配置标头

I trying to get the ajax function to request data from a url, however the scripts throws the error alert. in the console log window, it shows the following message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://test.domain.com/test2.aspx. This can be fixed by moving the resource to the same domain or enabling CORS.

I added the following headers in my web.config page, but when I request from the host (test.domain.com/test2.aspx), I get the following message in my console log with the a 404 response - not found:

config headers in test.domain.com/test2.aspx

<system.webServer>
 <httpProtocol>
  <customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
 </httpProtocol>
</system.webServer>





Ajax功能



Ajax function

$(document).ready(function () {
     $.support.cors = true;
     $.ajax({
         type: "Post",
         crossDomain: true,
         contentType: "application/json; charset=utf-8",
         url: "http://wwww.test.domain.com/test2.aspx/BindDatatable",
         data: "{}",
         dataType: "json",
         success: function (data) {
             alert(data.toSource());
             console.log(data);
             var myData = JSON.parse(data.d)
              for (var i = 0; i < myData.length; i++) {
                  $("#tbDetails").append("<tr><td>" + myData[i].Name + "</td><td>" + myData[i].Loan + "</td><td>" + myData[i].Evnt + "</td></tr>");
             }
         },
         error: function (result) {
             alert("Error");
         }
     });
 });





服务器端代码(test.aspx.cs):



server side code (test.aspx.cs):

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod()]
    public static string BindDatatable()
    {
        DataTable dt = new DataTable();
        List<UserDetails> details = new List<UserDetails>();

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["#####"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("######", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach (DataRow dtrow in dt.Rows)
                {
                    UserDetails user = new UserDetails();
                    user.Name= dtrow["###"].ToString();
                    user.Loan = dtrow["###"].ToString();
                    user.Evnt = dtrow["###"].ToString();
                    details.Add(user);
                }
            }
        }
         JavaScriptSerializer serializer = new JavaScriptSerializer();
         return serializer.Serialize(details);
    }

   public class UserDetails
    {
        public string Deal { get; set; }
        public string LoanProperty { get; set; }
        public string Events { get; set; }
    }





如何通过使用url主机调用数据来使我的功能正常工作?



谢谢



How can I make my function work, by using the url host to call the data?

Thank you

推荐答案

document )。 ready( function (){
(document).ready(function () {


.support.cors = true ;
.support.cors = true;


.ajax({
type: Post
crossDomain: true
contentType: application / json; charset = utf-8
url: http://wwww.test.domain.com/test2.aspx/BindDatatable
data: {}
dataType: json
成功:功能(数据){
alert( data.toSource());
console .log(data);
var myData = JSON .parse(data.d)
for var i = 0 ; i< myData .length; i ++){
.ajax({ type: "Post", crossDomain: true, contentType: "application/json; charset=utf-8", url: "http://wwww.test.domain.com/test2.aspx/BindDatatable", data: "{}", dataType: "json", success: function (data) { alert(data.toSource()); console.log(data); var myData = JSON.parse(data.d) for (var i = 0; i < myData.length; i++) {


这篇关于跨域ajax功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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