2

I am trying to make a cross domain request to send/recieve some data. I can't get past the object error. Before I was getting the No Transport Error but adding Query.support.cors = true; solved this issue.

var url = "http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract";
            $.ajax({
                type: 'GET',
                url: url,
                data: 'tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation', 
                datatype: "jsonp",
                contentType: "application/json",
                success: function (response) {
                    alert(response.data);
                },
                error: function (error) {
                    alert(error.statusText);
                }
            });

If I type the url out in a browser:

 http://CROSSDOMAINSERVER:PORT/Service1.svc/GetDataUsingDataContract?&tool=1&product=Some%20Product&details=9&bogus=should%20not%20pass%20validation

I get the correct response.

{"d":{"__type":"ClientSideData:#ProdResourceToolService","details":"9","product":"Some Product","result":"1","site":"Somewhere, SD","systime":"2\/6\/2013 2:50:20 PM","team":"0000000000","tool":"1","user":"username"}}

When I use ajax it does not submit it to the database or return data, just object error. Does anyone have any suggestions on how to get around this?

I should also specify if I remove http://CROSSDOMAINSERVER:PORT/ from var url when debugging locally I get the correct json response. Why does cross domain not work?

8
  • 1
    That response is not correct if you really want to do JSONP. The return value from JSONP is supposed to be JavaScript code for a function call, not just plain JSON. Commented Feb 6, 2013 at 21:11
  • From the last line of the question, it looks like you're running this from the same domain! Are you running this from http://localhost/? Commented Feb 6, 2013 at 21:14
  • jsonp is the only way to do a cross domain request though right? Commented Feb 6, 2013 at 21:14
  • Where did you get the idea of changing $.support.cors to true? If a browser doesn't support cors, making it use cors won't make it any better. Commented Feb 6, 2013 at 21:20
  • 1
    If you are doing JSONP, you don't need CORS. If you are doing CORS, you don't need JSONP. using them together is just confusing. Commented Feb 6, 2013 at 21:22

1 Answer 1

3

This is your current response:

{
    "d": {
        "__type": "ClientSideData:#ProdResourceToolService",
        "details": "9",
        "product": "Some Product",
        "result": "1",
        "site": "Somewhere, SD",
        "systime": "2/6/2013 2:50:20 PM",
        "team": "0000000000",
        "tool": "1",
        "user": "username"
    }
}

This is a valid JSON string. However, its not valid JSONP. If possible, make your service wrap the json in a function:

responseFunc({
    "d": {
        "__type": "ClientSideData:#ProdResourceToolService",
        "details": "9",
        "product": "Some Product",
        "result": "1",
        "site": "Somewhere, SD",
        "systime": "2/6/2013 2:50:20 PM",
        "team": "0000000000",
        "tool": "1",
        "user": "username"
    }
});

And in your $.ajax() call, add a property: jsonpCallback: 'responseFunc'.

Also, I would suggest passing the data as an object:

data: { tool: 1, product: 'Some Product' } //etc...

If you can access the service from the serverside without any issues you could create a httphandler (.ashx for example) and let it generate the JSON for you. Then you wouldn't have to deal with the cross domain issues on the client side.

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

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.