1

I have an issue calling web service which is in cross-domain. I've read some articles here about it, but I didn't really find a solution. I've just understood that I need the json format of the data, because I was always getting Error: Access denied. while trying to get xml data from service, but now I have a different problem. Here is my .ajax() call:

$.ajax({
            type: "GET",
            contentType: "application/jsonp; charset=utf-8",
            url: "http://tomas/_vti_bin/EmmaService.asmx/GetResult",
            dataType: "jsonp",
            data: {
                value : "testValue",
                converstionId : "testId"
            },
            success: function(resp) {
                alert("success: " + resp);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("error status: " + xhr.status);
                alert("error status text: " + xhr.statusText);
                alert("error response text: " + xhr.responseText);
            },
        });

From this I get error with 3 following alerts:

error status: 200
error status text: success
error response text: undefined

What I don't understand is error status text: success.

Code in my web service:

[WebMethod(EnableSession = false, Description = "Gets result")]
    public EmmaServiceResult GetResult(string value, string converstionId)
    {
        ...
        return result;
    }

Any suggestions on how to get this working? Thanks! :)

Gintas K
  • 1,438
  • 3
  • 18
  • 38
  • Note: I can't change the web service, it is being used by other solutions, and I need to get it called correctly and working without any code behind, just javascript, because I'm creating a SharePoint 2013 App, which doesn't allow code behind :/ – Gintas K Aug 02 '13 at 14:17
  • Can you run this inside Firebug on Firefox and/or Chrome Web Inspector, and then cut and paste the entire request (in text) and response (ditto) into the question? – deitch Aug 02 '13 at 14:19
  • Check the javascript console and network tab in your browser's dev tools. Look for any errors, and look at the xhr response to see if it is what you expect. – Jason P Aug 02 '13 at 14:19
  • I get `Uncaught TypeError: Object [object Object] has no method 'autocomplete'` – Gintas K Aug 02 '13 at 14:25

2 Answers2

2

Try adding ?callback=? to the end of your URL:

http://tomas/_vti_bin/EmmaService.asmx/GetResult?callback=?

Also, try looking at the thrownError to determine what the error is:

alert("error response text: " + thrownError);

It could be a parsing error, etc.. something not actually related to the ajax request, but how you define how the response should be handled.

Also, look here to see how to return json from a WCF service.

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]
Community
  • 1
  • 1
Tim Mac
  • 1,149
  • 1
  • 8
  • 17
  • I've changed it to `xhr.thrownError` and it still returns `undefined` :/ – Gintas K Aug 02 '13 at 14:22
  • now I get `error response text: jQuery...lots of numbers... was not called` – Gintas K Aug 02 '13 at 14:26
  • yeah, try changing from `jsonp` to `json`, that might get us a little closer – Tim Mac Aug 02 '13 at 14:28
  • now I get `error status: 0 and the response text: Access denied.` – Gintas K Aug 02 '13 at 14:30
  • the web service you're calling is on a different domain? – Tim Mac Aug 02 '13 at 14:33
  • yes it is, I also tried with specifying credentials on ajax call, still no success :/ – Gintas K Aug 02 '13 at 14:34
  • no problem :) I think that the problem is that my web service is not compatible with jsonp somehow, basically it was created to return xml, and worked fine when it was called from sharepoint webpart's code behind, and now I need to call it only with javascript, because i'm creating an app for sp2013 :/ dunno what to do at all :D – Gintas K Aug 02 '13 at 14:38
  • So I guess I'll need to modify a web service, what I didn't want to do, because then i'll have 2 web services with same usability, just compatible to different calls :D Thanks for your time anyway, got me on the road a bit with that `thrownError` :) +1 – Gintas K Aug 02 '13 at 14:42
  • Dam, I don't have any votes left for today, wait till tomorrow then :) – Gintas K Aug 02 '13 at 14:43
0

I recently had a lot of issues at work making a cross-domain request from an AJAX call. We ended up getting it working without having to modify the API, but we did need access to the server hosting the API so we could have it send down some headers in the response. But the whole issue was a pain to debug, and I found that all browsers were terrible about reporting meaningful errors. So potentially this might not work for you and apologies in advance if this doesn't address your issue.

The solution requires you make a CORS request, and add some headers to your server response. These pages were both good resources:

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS http://www.html5rocks.com/en/tutorials/cors/

I think in your case, since you're making a basic request and you're not dealing with cookies, you can leave your .ajax call essentially unchanged, just changing dataType to "json" and contentType to "application/json" if you're sending JSON.

You'll then have to modify the server to have it handle the CORS preflight request by adding these headers to the response:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Content-Type

(See this question: jQuery CORS Content-type OPTIONS)

Hopefully this will work for you!

Community
  • 1
  • 1
Justin Lang
  • 591
  • 1
  • 3
  • 17