0
$.ajax({ url: 'https://XXXXX.desktop.XXXX.com:9011/iws-merchant/XXXXX.htm', dataType: "jsonp", success: function (response) { str=response; }, error: function( response ) { alert( "ERROR: " + JSON.stringify ); } }); 

It is always going in error block. I am making an AJAX call to a different PORT(Same Domain).

But when i try to hit the same URL in new tab. I am able to see the response.

Any help will be highly appreicated.

10
  • domain, protocol and port have to match. Otherwise the request will fall under the SOP restrictions. Commented Mar 11, 2013 at 11:14
  • Is the server set up correctly to return a JSONP response? In your example, since you didn't specify a callback parameter, the GET parameter sent to the server would be called 'callback'. Commented Mar 11, 2013 at 11:16
  • so we can use JSONP ryt? Commented Mar 11, 2013 at 11:17
  • BTW in your error block JSON.stringify by itself isn't going to do anything...I assume that was just a typo Commented Mar 11, 2013 at 11:19
  • dataType:"jsonp" will do for me ryt? when the response is succesfull it will come to success handler. Commented Mar 11, 2013 at 11:23

2 Answers 2

3

you can use JSONP as Gaurav Agrawal suggested OR you can enable the Access-Control-Allow-Origin for the site who receives ajax request.

Ajax works like this: Same domain but different port = different domain

if you are using asp.net on your ajax target server you can enable access control adding this in web.config:

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

and do yourself a favor and replace "*" with your site url!

in some situation you can need even those keys, just google every function before adding it!

<add name="Access-Control-Allow-Headers" value="*" /> <add name="Access-Control-Allow-Methods" value="*" /> <add name="Access-Control-Allow-Credentials" value="true" /> <add name="Access-Control-Expose-Headers" value="*"/> 
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot make cross domain AJAX calls using JSON. You need to use JSONP. So instead of returning a regular JsonResult from your controller action write a custom action result that will wrap the JSON in a callback that is passed as parameter:

public class JsonpResult : ActionResult { private readonly object _obj; public JsonpResult(object obj) { _obj = obj; } public override void ExecuteResult(ControllerContext context) { var serializer = new JavaScriptSerializer(); var callbackname = context.HttpContext.Request["callback"]; var jsonp = string.Format("{0}({1})", callbackname, serializer.Serialize(_obj)); var response = context.HttpContext.Response; response.ContentType = "application/json"; response.Write(jsonp); } } 

and then have your controller action return this custom action result:

public ActionResult SomeAction() { var result = new[] { new { Id = 1, Name = "item 1" }, new { Id = 2, Name = "item 2" }, new { Id = 3, Name = "item 3" }, }; return new JsonpResult(balances); } 

Now you could consume this action cross domain:

var url = "http://example.com/SomeController/SomeAction/"; $.getJSON(url + '?callback=?', function (data) { alert(data); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.