0

This will be an easy one for you, but after ages of looking online I can't find a solution, or at least one I'm understanding.

result and users variables both alert correctly. The data contents shows empty - why?

How should I be inserting these two variables into data ?

(also if it is right - how do I view the data content without going into server side)

Thank you in advance.

        var result = result.text;
        var users = localStorage.getItem('user');
         alert("1");
               $.ajax({
   url:'********',
   method: 'POST',
   data: {user: users, serial: result},
   dataType: "text",
   contentType: "application/json",
   success: function(data){


       alert (data);
       alert(users);
       alert(result);
2
  • Include server side code also Commented Sep 14, 2017 at 17:50
  • It's just the sending part at the mo - I want to know exactly what's being sent before being handling it. Commented Sep 14, 2017 at 17:52

1 Answer 1

1

In your code alert(data) will refer to the data returned by the server. Not the data of your ajax request.

$.ajax() takes a settings object as its parameter. It's a set of key/value pairs that configure the Ajax request. data is just one of the keys of the object you are passing to the $.ajax() method.

To simplify your code, this is what is happening:

// this is what you're sending to the server
var dataSentToTheServer = {
    user: users,
    serial: result 
}

// creating an object where data, url, success etc are keys.
var settings = {
    url:'api/method',
    method: 'POST',
    data: dataSentToTheServer,
    dataType: "text",
    success: function (dataSentFromTheServer) {
        alert(dataSentFromTheServer); // this argument will have the data returned by your server
        alert(dataSentToTheServer); // data which was sent *to* the server
        alert(data); // doesn't exist
    }
    ....
}

$.ajax(settings);
Sign up to request clarification or add additional context in comments.

11 Comments

Okay - thnks - what would be the best way of viewing the data key contents in that case?
@MarkD sorry I didn't see the data in your success callback. I updated the answer accordingly. In your success callback, data will refer to the data sent by the server.
ok thanks v much for that. I'm not completely following the answer. How can I view exactly what is sent, so I can handle it in php? I'm not sure how it will be delivered. In php I have $u1 = trim($_POST['user']); $duid = ($_POST['serial']); - would this be correct. The server is not responding - If I can see the fields I could figure it out
@MarkD This code means, {user: users, serial: result}: you are simply creating an object with 2 properties. Updated the answer on how to get the data sent to the server
Okay! I did as you said (in regards to viewing the going to server data)- and the response i got is [object Object] - any idea why looking at my code?
|

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.