简体   繁体   中英

How to use Azure mobile service REST api?

The new custom API script allows a lot of customization through any type of connection.

I found that this website Custom API in Azure Mobile Services – Client SDKs describes the custom API.

var client = new WindowsAzure.MobileServiceClient('http://myservice.azure-mobile.net/', 'mykey');
    client.invokeApi('query', {
        method: 'POST'
    });

But I couldn't run this code. It is supposed to show a message "Hello post World!". I put the code inside tags in an HTML file and ran it but nothing happened.

Any help?

The call you have is making a call to your service, but it's ignoring its response. Assuming you have a custom API called 'query' (since it's what you're passing to invokeApi ) with the following body:

exports.post = function(request, response) {
    response.send(200, { message: 'Hello world' });
};

Your client code is calling it and (if everything goes fine) receiving the response, but it's not doing anything with it. There are a couple of ways to find out whether the call is being made. For example, you can add a log entry in the API and check the logs in your service:

exports.post = function(request, response) {
    console.log('The API was called');
    response.send(200, { message: 'Hello world' });
};

Or you can use a networking tool (the browser developer tools or Fiddler, for example) to see if the request is being made. Or you can actually do something with the result in the client side:

var client = new WindowsAzure.MobileServiceClient('http://myservice.azure-mobile.net/', 'mykey');
client.invokeApi('query', {
    method: 'POST'
}).done(
    function(result) { alert('Result: ' + JSON.stringify(result)); },
    function(error) { alert('Error: ' + error); }
);

One thing which you need to look at if you're calling the API from a browser is whether the domain from where the page is being loaded is in the 'allow requests from host names' list, under the 'configure' tab, 'cross-origin resource sharing (cors)' section. If it's not, then you may get an error instead of the response you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM