How to use the $.ajax function date parameter

Asked

Viewed 7,594 times

3

It is possible to send two JSON at the same time as the parameter value date jQuery.ajax()?

Is there any way to call these two arguments, since I need to call these two json and show on screen?

Follow the code below:

var token = "Xke;
var jsonTipo1 = "{ tipo: 1, numPagina: 1 }";
var jsonTipo2 = "{ tipo: 2, numPagina: 1 }";

$.ajax({

    type : "POST",
    url : "https://producao-ws.talcoisa.net/api/Produto/conteudo/",
    processData : false,

    data : json, jsonTipo2,  
    success : sucesso,
    error : errar,

    beforeSend : function(xhrObj) {
        xhrObj.setRequestHeader("tokenApp", token);
        xhrObj.setRequestHeader("Content-Type", "application/json");

    },
});
//Ws

// Responsável por chamar metódos responsáveis pelos títulos e imagens  que vem do json

function sucesso(jsonTipo1 , jsonTipo2) {

    mostrarImagemNaTelaJsonTipo1(json);

    mostrarTituloNaTelaJsonTipo1(json);
    mostrarImagemNaTelaJsonTipo2(jsonTipo2);

}



function errar(json) {

    ToastMessage.showMessage("Fail!");
}
  • What should these be jsonTipo1 and jsonTipo2? The second parameter of callback success is the textStatus.

  • type 1 refers to those images and type 2 other images, the two are part of the same Json. The idea is to update these images within html.

  • In your callback sucesso the parameter jsonTipo2 will contain the textStatus returned from the ajax request. Your question is really very confusing. Try to rewrite and say exactly what is going wrong and what is the expected outcome.

  • 1

    To send two JSON through the same request, you must merge them through the method Concat(). But you’re confusing what each argument jQuery.ajax() represents. Read the documentation again and see that they are not your JSON. Whatever you "show on screen" will be what the server language sent as output (eg a PHP echo/print).

2 answers

4

Send an array of objects:

var token = "Xke;
var jsonTipo1 = "{ tipo: 1, numPagina: 1 }";
var jsonTipo2 = "{ tipo: 2, numPagina: 1 }";
var dados = [jsonTipo1, jsonTipo2]; // colocando ambos numa array

$.ajax({

    type : "POST",
    url : "https://producao-ws.talcoisa.net/api/Produto/conteudo/",
    processData : false,

    data : dados, // passando a array
    success : sucesso,
    error : errar,

    beforeSend : function(xhrObj) {
        xhrObj.setRequestHeader("tokenApp", token);
        xhrObj.setRequestHeader("Content-Type", "application/json");

    },
});

To return more than one object, use the same logic, and in callback the data will always come as the first argument:

function sucesso(arrayDeDados) {

}
  • Something like: Success (data) Function{something.data.jsonTipo1.Product[1]. imageDoProduct}

  • I didn’t understand your comment above

  • I didn’t understand the function success(arrayDeDados); I want to call one of the positions of the array and show on the screen, is it possible? I tried here but I believe I did wrong.

  • I put a link in Gist with the code, please see. https://gist.github.com/anonymous/1f5e6796758375eb57d0

0

Replace with:

data : { json, jsonTipo2 },
  • 2

    Could pq this change works, it makes it easier for other people to understand, if you have any additional reference in the reply. Stackoverflow works different from a forum, see the differences in tour the explanation is very quick.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.