3

I want to access a JSON file which is in domain1 (example.com) from domain2 (example2.com). For example,

$.ajax({
    type:'get',
    url: 'http://example.com/vigneshmoha.json',
    success: function(data) {
        console.log(data);
    },
    statusCode: {
        404: function() {
            console.log('Status code: 404');
        }
    }
}); 

I want to make this ajax request to example.com from some other domain (ie) example2.com.

I have tried JSONP. I couldn't understand how it works. Can someone please explain the way its work?

3
  • 1
    Does the domain serving the JSON actually support JSONP? (Or is it your domain so you can make it support JSONP?) Commented Oct 21, 2013 at 8:55
  • It's my domain only. How can i make it support jsonp? Commented Oct 21, 2013 at 9:00
  • Sorry, I don't really have the patience to write a tutorial from scratch when there are any number of "how to implement jsonp" tutorials already out there on the web. (E.g., here's one.) Commented Oct 21, 2013 at 9:22

2 Answers 2

9

Your service has to return jsonp, which is basically javascript code. You need to supply a callback function to the service from your ajax request, and what is returned is the function call.

Below is a working example.

ajax request:

$.ajax({
            crossDomain: true,
            type:"GET",
            contentType: "application/json; charset=utf-8",
            async:false,
            url: "http://<your service url here>/HelloWorld?callback=?",
            data: {projectID:1},
            dataType: "jsonp",                
            jsonpCallback: 'fnsuccesscallback'
        });

server side code returning jsonp (c#):

public void HelloWorld(int projectID,string callback)
    {

        String s = "Hello World !!";
        StringBuilder sb = new StringBuilder();
        JavaScriptSerializer js = new JavaScriptSerializer();
        sb.Append(callback + "(");
        sb.Append(js.Serialize(s));
        sb.Append(");");
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.Write(sb.ToString());
        Context.Response.End();
    }

Refer What is JSONP all about?.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. It helped a lot. Following link contains my snippet using jquery and php. [pastebin.com/kWnWVXDh]
hey @Saranya, can u share any link complete demo! it will be really helpful! plz
hey @Saranya , how about the post json-p, could you elaborate more about it.
-1

Have you tried calling it as:

$.getJSON('http://example.com/vigneshmoha.json?callback=foo', null, function(data) {
          console.log(data);
});

And see what happens?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.