4

Pass js object with ajax request

 3 years ago
source link: https://www.codesd.com/item/pass-js-object-with-ajax-request.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pass js object with ajax request

advertisements

This question already has an answer here:

  • jQuery posting valid json in request body 1 answer

I need to send a js object to the server through ajax request; is an object containing parameters for a sql query with Sequelize orm in node js; an example is like this:

var data =
{
    include: [
        { model: model.Shop },
        { model: model.Product,
            include: [
                { model: model.File }
            ]
        }
    ]
}

It can contain arrays of objects nested on multiple levels; before sending it I can convert it to valid JSON if needed, like this:

var data =
{
    "include": [
        { "model": "model.Shop" },
        { "model": "model.Product",
            "include": [
                { "model": "model.File" }
            ]
        }
    ]
}

I've tried to send it as JSON:

$.ajax({
    //...
    data: data
});

The problem is that when in the node server I do JSON.parse of the received string, the value of each property is a string and it is not recognized as a model object;

How can I make my server able to understand this?


Try to use JSON.stringify

$.ajax({
    data: JSON.stringify(data)
});

Related Articles

How to POST an array of objects with $ .ajax (jQuery or Zepto)

I'd like to POST an array of objects with $.ajax in Zepto or Jquery. Both exhibit the same odd error, but I can't find what I'm doing wrong. The data saves to the server when sent using a test client like 'RestEasy', and I can see the request getting

What is this type of javascript? And help with AJAX request

I recently asked this question: Dynamically Query a Database to check for value And I got an awesome and informative answer. But whenever I search on Google to find out more about "Ajax Requests" the codes never look similar to the one provided.

Pass javascript objects with parentheses as PHP keys

Assume I have this object in JavaScript which is built like this: var obj = {}; var fields = ['table_name[field_name]', 'tale_name[field_name_2]'] for(var i; i < fields.length; i++){ obj[fields[i]] = someBulkOfData; } when logged in the console, obj

How to send a JS object with ajax

I have the following Jquery code that sends a ajax request to add-to-cart.php. var productData = { "product_id": s_product_id, "product_opties": s_product_opties, "product_aantal": s_product_aantal } productData = JSON.string

Difficulties with ajax request

I followed this lesson (video/code) to make confirmation box and I wanted to add an AJAX request for deleting data from my database. However I just can't get it to work. Here is my AJAX code that I added to the deletePost function: $.ajax({ type: "PO

Using Authentication with Ajax.Request

I currently have a Palm WebOS application that uses an Ajax.Request to connect to a web service using basic authentication. To send the username and password, I simply include it in the url (i.e. http://username:password@ip-address:port/) which works

Send javascript object with AJAX

This question already has an answer here: Passing JavaScript array to PHP through jQuery $.ajax 8 answers I am building a platform where users can draw graphs. In order to do this, I am using Chart.js and each time a graph is created, I save an insta

Race condition related to Autoload in cucumber with AJAX requests

I'm using Cucumber with capybara-webkit for my app's integration tests on Ruby 2.0.0, Rails 4.1. A handful of test in my cucumber test suite unexpectedly began spitting out errors like this: Circular dependency detected while autoloading constant UiV

Passing several parameters with $ .ajax url

I am facing problem in passing parrameters with ajax url.I think error is in parametters code syntax.Plz help. var timestamp = null; function waitformsg(id,name) { $.ajax({ type:"Post", url:"getdata.php?timestamp="+timestamp+"uid=

Laravel - I succeed the beginning and the end of the price range with ajax request but I can not come back the right way

Array Result after click price rangeHere I am trying to fetch products by passing price range inputs to controller function: <script> function send(){ var start = $('#amount_start').val(); var end = $('#amount_end').val(); //var dataString = "s

How do you deal with AJAX requests when the user session expires or when the request ends with a 302

Other questions similar have been asked, but they didnt tend to deal with a generic multi purpose solution. They tended to deal only with user authentication issues. Sometimes a 302 is valid for other circumstances. So how can you create a generic 30

How can I pass more parameters with AJAX

I have the following: $('#TopicDescription').change(function () { $.ajax({ url: "/adminQuestions/GetSubTopics", data: $('#TopicDescription').val(), success: function (data) { $('#SubTopicDescription').html(data); } }); }); This works good but I

How to save a session on a remote server with ajax request javascript / PHP

I start my server at http://localhost:8080, and try to query with ajax server at http://192.168.1.2/. I need to do samething to send set cookie command to store cookies at 192.168.1.2 server, and get information with ajax from it at localhost server.

Pass the object with the object method to work

I know that in Python that if, say you want to pass two parameters to a function, one an object, and another that specifies the instance method that must be called on the object, the user can easily pass the object itself, along with the name of the

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK