This project is read-only.

SOA:How to use javascript calling the rest service?

Nov 10, 2013 at 5:42 AM
hi:
How to use javascript calling the rest service?  
My service can not open in IE. Is the code framework's service can just be called by C# appliction?
Nov 10, 2013 at 7:04 AM
who can help me ?
Nov 10, 2013 at 6:01 PM
Hello!

I assume you are building a standard service with CODE Framework and then hosting it as REST/JSON in the self-hosting mode, right?

The services that are created in this fashion are standard REST services that can be called by posting to them (Very = Post). That should really be the only thing you specifically have to know about the service.

What does however give people difficulty when calling these REST services (the same is true for REST services in general, btw), is that they may not be hosted on the same domain as your web site, in which case your browser may not allow the call to go through. We have thought about ways to help people call REST services (again: any REST service, not just those you create yourself) in those cases and you will probably see some components in CODE Framework that make that simpler in the future.


Markus
Nov 11, 2013 at 2:40 AM
As Markus noted, there is nothing really special about a REST/JSON CODE Framework endpoint. From IE you would make an AJAX call, similar to this:
$.ajax({
    type: "GET",
    url: << your service url and method>> + <<optional parameter>>,
    contentType: "application/json; charset=utf-8",
    crossDomain: false,
    cache: false,
    dataType: 'json',
    data: { "Optional": "JSON Payload" },
    success: function(data) {
        if (data.Success) {
            << do something here with the results >>
        }
    },
    error: function(xhr2, status2, error2) {
        << handle an ajax error >>
    }
});
Now the trick does come in with the cross domain calls (CORS). Sometimes the crossDomain: true will help, and there are a couple of other settings (can't think of them right now). CORS will come into play if you host your services on a different domain than the website. So if your services are http://service.mysite.com and your web is http://www.mysite.com then CORS will kick in.

Hope this helps,
Jeff
Nov 11, 2013 at 9:58 AM
Thanks so much all of you. As I understand about the REST Webservice ,The Get method can be opened in the explorer. In my project,
I use the CODE Dev host style to debug my service. In the CODE Dev's form I can found the method and invoke it successful.The Service Url : http://localhost/dev/IVehicleService/rest/json
 But when I called my method like this:
 _http://localhost/base/IVehicleService/rest/json/Vehicle_  or
_http://localhost/base/IVehicleService/Vehicle/rest/json_  
It always failed,and display not allowed method. 
I dont't know what is the correct way to call it in the explorer directly or use the Ajax.
Nov 11, 2013 at 5:18 PM
Actually, Jeff has a typo in his example above. It is "POST" not "GET". You can't just do a GET on the services. This is a deliberate design choice.

Markus
Nov 12, 2013 at 1:58 AM
Edited Nov 12, 2013 at 2:00 AM
Hi MarkusEgger:
  1. What is the right URL of my method when I POST?
    For example:
    The service Url is:http://localhost/base/IVehicleService/rest/json,My method is GetVehicle(). How to call it ?
    Whatever I try ,It always 404 error.
  2. The WCF4.0 have some changes for Rest, how to use codeframework establish a standard Rest Service?
Nov 12, 2013 at 4:51 AM
Let me start answering the second question first: When you want to create a "standard" REST service, I assume you mean creating a REST service that explicitly uses the features for REST such as adding the REST attributes and URL mapping and such. For that, you do not need to do anything special. In fact, CODE Framework doesn't really play into that. In fact, you might even want to consider using WebAPI instead of WCF4 since that seems to be the more popular way to go.

The reason to use CODE Framework would be if you want to create a service you can host pretty much in any standard, including REST, but also others. CODE Framework is good at that. If on the other hand, you want a service that is REST only, then the standard features in WCF and WebAPI are very good and you should just use those.

Now as far as the URL goes: if your basic URL is http://localhost/base/IVehicleService/rest/json and your method is GetVehicle, then you should post to http://localhost/base/IVehicleService/rest/json/GetVehicle (so the method name is added after the regular URL).


Markus