Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

How to call Cross Domain Ajax using Jquery

$
0
0

Deal all  i have two website (eg: website1 and website2) website1 contain some resource (webservice)  the need to process by website2

The source code of website 1 is given here

****************************Start of Source code [Website-1]************************

[Serializable]
public class Car
{

    public string Name { get; set; }
    public string Brand { get; set; }
   
   

}

public class DataStore {

    public Car[] GetCars()
    {

        Car[] cars ={new Car{Brand="Ford",Name="X10"},
                        new Car{Brand="Renult",Name="Scala"},new Car{Brand="Nissan",Name="Sunny"}};
        return cars;

    }
}

Generic Code Handler ( Important Here i am using Newtonsoft Json library for serilize)

Code is given below

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]


public class CorssDHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
       
        if (!string.IsNullOrEmpty(context.Request.QueryString["callback"])){

            context.Response.ContentType = "application/json ";
            string p = Newtonsoft.Json.JsonConvert.SerializeObject(new DataStore().GetCars(), Newtonsoft.Json.Formatting.Indented);
     
            context.Response.Write(p);
      
        
        
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

**************************************** End of Source code Website1******************************

Here from website 2  i am using jquery to ajax the javascript is given below

$(document).ready(function () {

  var pageUrl = "http://localhost:56670/Website1/CorssDHandler.ashx?callback=?"

    $.ajax({
        type: "GET",
        url: pageUrl,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: successFunction,
        error: onErrorCall

    });



});

function successFunction(){
alert("done");
}
function onErrorCall(){

alert("Fire");
}

But it always showing   my error message fire

I checked with Firbug it give the given feed backe when the request made

In myresponse Head it show like

[
  {
    "Name": "X10",
    "Brand": "Ford"
  },
  {
    "Name": "Scala",
    "Brand": "Renult"
  },
  {
    "Name": "Sunny",
    "Brand": "Nissan"
  }
]

There is another tab in firbug with name JSON  and containing follwoing data

0
	Object { Name=

"X10"

, Brand=

"Ford"

}
	
Name
	

"X10"

	
Brand
	

"Ford"

	
1
	Object { Name=

"Scala"

, Brand=

"Renult"

}
	
Name
	

"Scala"

	
Brand
	

"Renult"

	
2
	Object { Name=

"Sunny"

, Brand=

"Nissan"

}
	
Name
	

"Sunny"

	
Brand
	

"Nissan"

And status 200 OK

Whats wrong with my code need some urgent attention  ( I changed both GET adn POST verb)





Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>