L
L
LB7772015-04-03 11:41:46
React
LB777, 2015-04-03 11:41:46

How to correctly make ajax communication with the server in react.js?

Our server is built on .NET, so I need to set up ajax in react.js.
I tried to use ajax from jquery, but for some reason it does not work without beforeSend in which the DOM access via jquery must be written (I don’t know what kind of magic it is, but when you write like $(“.class”).after(“block html”); then the server is contacted, if this is not there or even the entire beforeSend object is not there, then it falls into an error).
I recently started looking into XMLHttpRequest, which I thought would be more appropriate to use in react.js.
In the examples of communication with the server, I saw these two ways.
I'm wondering which one is better to use?
And which one do you use the most?
At the moment I am implementing an authorization form and I need to work with the following data:
Type: "type of request"
Url: "url"
Headers: "headers"
beforeSend: "what will be processed before starting the request"
success: "data that will come from the server if successful"
error: "any errors"
complete : "something will be done here when the request completes"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
123 123, 2015-04-03
@LB777

AJAX in jQuery is just a wrapper over XHR, I would say an easier wrapper to use. Therefore, it all depends on whether you are writing a project with or without jQuery.
In React, more specifically in Flux, it doesn't matter how you make the request. Working with network I / O is taken out in a separate module that receives events from React components, receives data from your API and updates the stores.

P
PQR, 2015-04-03
@PQR

I tried to use ajax from jquery, but for some reason it does not work without beforeSend in which the DOM access via jquery must be written (I don’t know what kind of magic it is, but when you write like $(“.class”).after(“block html”); then the server is contacted, if this is not there or even the entire beforeSend object is not there, then it falls into an error).

This is some kind of unprecedented garbage, it should not be like this.
1. beforeSend is not required to work with ajax functions in jQuery
2. working with the DOM (whether it is in beforeSend or somewhere else) does not affect the ajax functionality in jQuery in
any way 3. using react.js also does not does not affect the ajax functionality in jQuery
In general, you are going somewhere in the wrong direction in search of the cause of the error.
Can you show me the text of the error or a code fragment in which you send an ajax request to the server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question