2

I need to do ajax to javascript and get a result by calling js function. but I cannot get data.

I'm making TIZEN web application. and I have web service on my server(asp). I did ajax to my web but I got error. As you can see, I Tried to debugging through console.log but any reasonable values not printed.

app.js (90) :getAlarmData()

app.js (153) :

$.ajax({
        type: "POST"
        , url: "serverIP/WebProject/WebContents/view/filename/function name"
        , data: null
        , contentType: "application/json; charset=utf-8"
        , dataType: "json"
        , async: false
        , success: function (jSonResult) {
       },
        error: function (xhr, status, error) {
            console.log(error + "\n" + status + "\n" + xhr.responseText);
        }
    });

app.js (90) :getAlarmData()

app.js (153) :

2
  • 1
    Is your browser showing CORS error? I assume you're testing on a locally deployed version of your app and your serverIP is not that same as your application host Commented Jul 11, 2019 at 4:31
  • What error did you get? Commented Jul 11, 2019 at 4:34

1 Answer 1

1

You are trying to do a function call on a file which is present on your server, you can't do that directly. You have to open a road on your server which return the result of your function.

You have to take look about communicate a single page application (SPA - Front end app) and your API (server side app).

For example :

$.ajax({
    type: "POST"
    , url: "serverIP/my/awesome/road/which/calling/my/function"
    , data: null
    , contentType: "application/json; charset=utf-8"
    , dataType: "json"
    , async: false
    , success: function (jSonResult) {
        console.log( jSonResult );
    },
    error: function (xhr, status, error) {
        console.log(error + "\n" + status + "\n" + xhr.responseText);
    }
});

For resume, you have to :

  • Open a road on your server
  • Return the result of your function in the call (jSonResult)
  • Use it in the success callback
Sign up to request clarification or add additional context in comments.

2 Comments

What does "Open a road on your server" means? There are no firewall blocking, and I can connect my web service on my PC.
You can learn about it here, it talking about express.js which is a library on node.js which help you for build a server http : expressjs.com/en/guide/routing.html As you can see in the documentation, you open an URL road "/" for example, you will call it with ajax and get the return in the success function.

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.