1

I want to make a request http Webservice API to

https://sms.vietguys.biz/api/u=[user]&pwd=[password]&from=[Title]&phone=[phonenumber]&sms=[ContentSMS]

using javascript.

Can I do this on Sharepoint Online ?

When I handle by below code:

var urlstring =`https://sms.vietguys.biz/api/u=[user]&pwd=[password]&from=[Title]&phone=[phonenumber]&sms=[ContentSMS]`;
$.ajax({
    url: urlstring,
    success: function (data) { 
        //do smth with data
    },
    error: function(a,m){
       //process error
    }

I got this error:

XMLHttpRequest cannot load .No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://benhvienthuduc.sharepoint.com' is therefore not allowed access. The response had HTTP status code 404.

  • that error suggests that you need to enable cors for your service, this article should help with that. – garglblarg Oct 5 '16 at 7:48
0

You can use ajax:

$.ajax({
    url: "https://sms.vietguys.biz/api/u=[user]&pwd=[password]&from=[Title]&phone=[phonenumber]&sms=[ContentSMS]",
    success: function (data) { 
        //do smth with data
    },
    error: function(a,m){
       //process error
    }
});
| improve this answer | |
0

The error you are getting is because of security restriction that prevents requests being made from one domain to another ie from xxx.sharepoint.com is requesting sms.vietguys.biz . To overcome this a commonly used practice is CORS and another method is by using JSONP

| improve this answer | |
  • Can you take me a example about CORS, plzz? sms.vietguys.biz is another organization, i can't using JSONP, because i can't code server side. – Văn Trịnh Oct 5 '16 at 8:18
  • But if that api is not configured for cross domain calls then you cannot consume it from another domain – Unnie Oct 5 '16 at 8:30
0

As @Unnie inform that you have enable cross on server and if api server is not in your control then you cannot do it. for that you have to create proxy server who will take care CROS

There several options from that I will tell you two

  1. If You can use azure then use azure function where you can make api proxy and do setting for CROS
  2. Use https://cors-anywhere.herokuapp.com/ In your case

    $.ajax({ url: "https://cors-anywhere.herokuapp.com/https://sms.vietguys.biz/api/u=[user]&pwd=[password]&from=[Title]&phone=[phonenumber]&sms=[ContentSMS]", success: function (data) { //do smth with data }, error: function(a,m){ //process error } });

But I will not use second option in production environment

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.