Problemas de jQuery-form y Json

Good day guys, I'm using jQuery-form to submit a multipart form.

Este es mi código PHP:

foreach($addedData as $key => $value)
    $result[$key] = $value;

echo json_encode($result);

JavaScript:

function showResponse(responseText){
   alert(responseText);
}
$('button').click(function(){
    $("form").ajaxForm({
        success: showResponse,
        clearForm: true,
    dataType: "json",
    contentType: "application/json; charset=utf-8"
    });
});

With this, the response was [object Object]. But when I removed these two lines:

dataType: "json",
contentType: "application/json; charset=utf-8"

The response became:

{"key1":"val1","key2":"va2","key3":"val3"}

My questions are: Do I really have to remove those two lines? And how can I get the values of the responseText using Json?

preguntado el 31 de julio de 12 a las 11:07

If you want a traversable object returned, responseText is not the best name for the argument. It's an object (jQuery automatically parses it), not a string. -

5 Respuestas

Al tener el dataType:"json", .. you are telling the request that you want the returned item to be an object (like $.parseJSON(responseText);). Without it you will be given plain text.

To print the object you will need something like this:

function showResponse(responseObj){
   $.each(responseObj,function(i,v) {
         alert(i+' = '+v);
   });
}

Respondido 31 Jul 12, 11:07

Si especifica dataType: 'json' entonces tu success callback function will be passed the object the JSON represents, rather than a JSON string. jQuery implicitly handles converting the responseText to the object for you, so you don't have to do JSON.parse(responseText) for yourself. If the response isn't valid JSON, the success callback won't be executed; even if the AJAX request returned successfully.

To get the values out, just use responseText like any other Javascript object; though you may want to give it a different name to make it clearer what it actually is.

Respondido 31 Jul 12, 11:07

No you don't, but the alert-box can't display the JSON-object. Try doing Console.log(responseText) instead, and inspecting the result in the developertools in your browser. [object Object] is a object literal

Respondido 31 Jul 12, 11:07

If you remove the lines then you get string ('{"key1":"val1","key2":"va2","key3":"val3"}') in JavaScript (because it use respone as file with text/text mime tipe), if you don't then you get Object ({"key1":"val1","key2":"va2","key3":"val3"}), so I think you should not remove them, if you want loop though this for example. By the way, when you alert an object JavaScript convert object to string (always '[Object object]').

Respondido 31 Jul 12, 11:07

don't remove the below code from your script and add the below code you will get the alert correctly

dataType: "json",
contentType: "application/json; charset=utf-8"

var htmlstr;
for(var i in responsetext)
{
htmlstr += i +"="+responsetext[i]+"\n";
}
alert(htmlstr);

Respondido 31 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.