[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Помогите разобраться с jquery.form
HErATuB
Не магу разобраться с jquery.form. Мене нужно получить ответ от сервера но приходит в ответ null. Вот код:

<form id="shop_form_paramets" method="POST" enctype="multipart/form-data" action="/test/myshop.php">
<div
class="stuffbox">
<H3
class="hndle"><label for="link_name">Товар</label></H3>
<div
class="inside">
<table
class="links-table" cellspacing="0"><tbody>
<tr><th>
Активность</th>
<td><select
name="action" ><option value="1" >ДА</option><option value="0">НЕТ</option></select></td></tr>
<tr><th>
Марка</th>
<td><input
name="marka" type='text' /></td></tr>
<tr><th>
Модель</th>
<td><input
name="model" type='text' /></td></tr>
<tr><th>
Цена (руб)</th>
<td><input
name="praic" type='text' /></td></tr>
<tr><th>
Фото</th>
<td><input
name="photo" type='file' /></td></tr>
</tbody></table>
</div>
</div>
</div>
<div
class="metabox-holder columns-2">

<div
class="stuffbox">
<H3
class="hndle"><label for="link_name">Свойства товара</label></H3>
<div
class="inside">
<table
id="sv_tovar" class="links-table" cellspacing="0"><tbody>
<tr
id="shop_sv_0"><th><input name="na_sv[]" type='text' /></th>
<td><input
name="sv_sv[]" type='text' /></td><td></td></tr>
</tbody></table>
<input
onclick="add_shop_tovar()"; class="button-secondary action" type="button" value="+" />
</div>
</div>
</div>
<input
class="button-secondary action" name="go_shop" onclick="go_tu_form_shop()" type="submit"value="Сохранить" /> <input type="button" class="button-secondary action" value="Отмена">
</form>


Собственно сам скрипт отправки:

var go_tu_form_shop = function(){
$("#shop_form_paramets").ajaxForm({
dataType: "json",
success : function(text){alert("Запись добавлена");}
}
);

};

ответ сервера:

<?php
$json_data = array ('id'=>1,'name'=>"ivan",'country'=>'Russia',"office"=>array("yandex"," management"));
echo json_encode($json_data);
?>

И 2 вопрос отправит ли файл через jquery.form?



Спустя 4 минуты, 40 секунд (22.08.2012 - 10:49) Игорь_Vasinsky написал(а):
уф... ну это древний плагин... зачем он? JQuery итак работает и $.ajax и с getJSON()

ты же не принял даже response)) т.е ответ, ты просто

success : function(text){alert("Запись добавлена");}


а так

success : function(text){alert(text);}

Спустя 9 минут, 16 секунд (22.08.2012 - 10:58) HErATuB написал(а):
Цитата (Игорь_Vasinsky @ 22.08.2012 - 08:49)
success : function(text){alert(text);}

Забыл изменить) вот именно он выдает null

Спустя 11 минут (22.08.2012 - 11:09) Игорь_Vasinsky написал(а):
Цитата
// prepare the form when the DOM is ready
$(document).ready(function() {
    var options = {
        target:        '#output1',  // target element(s) to be updated with server response
        beforeSubmit:  showRequest,  // pre-submit callback
        success:      showResponse  // post-submit callback

        // other available options:
        //url:      url        // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit

        // $.ajax options can be used here too, for example:
        //timeout:  3000
    };

    // bind form using 'ajaxForm'
    $('#myForm1').ajaxForm(options);
});

// pre-submit callback
function showRequest(formData, jqForm, options) {
    // formData is an array; here we use $.param to convert it to a string to display it
    // but the form plugin does this for you automatically when it submits the data
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the
    // DOM element for the form do this:
    // var formElement = jqForm[0];

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted;
    // returning anything other than false will allow the form submit to continue
    return true;
}

// post-submit callback
function showResponse(responseText, statusText, xhr, $form)  {
    // for normal html responses, the first argument to the success callback
    // is the XMLHttpRequest object's responseText property

    // if the ajaxForm method was passed an Options Object with the dataType
    // property set to 'xml' then the first argument to the success callback
    // is the XMLHttpRequest object's responseXML property

    // if the ajaxForm method was passed an Options Object with the dataType
    // property set to 'json' then the first argument to the success callback
    // is the json data object returned by the server

    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
        '\n\nThe output div should have already been updated with the responseText.');
}


success:       showResponse  // post-submit callback 


function showResponse(responseText, statusText, xhr, $form)  { 
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property

// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property

// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server


alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}

Спустя 1 минута, 35 секунд (22.08.2012 - 11:10) HErATuB написал(а):
Я этим плагином воспользовался потому что нужно с формай отправить файл

Спустя 16 минут, 28 секунд (22.08.2012 - 11:27) HErATuB написал(а):
Что то не понятно, если отправляю в json ответ должен приходить в json. А приходит null

Спустя 59 минут, 31 секунда (22.08.2012 - 12:26) HErATuB написал(а):
Игорь_Vasinsky спасибо разобрался с этим вопросом)

Спустя 4 минуты, 35 секунд (22.08.2012 - 12:31) Игорь_Vasinsky написал(а):
ну озвучь smile.gif

Спустя 1 минута, 25 секунд (22.08.2012 - 12:32) Игорь_Vasinsky написал(а):
Цитата
если отправляю в json ответ должен приходить в json. А приходит null

а вот и нет)) ответ ты формируешь в обработчике - а там как фантазия, хочешь JSON, хочешь текст, хочешь XML
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2025 Invision Power Services, Inc.