www.webdeveloper.com
Results 1 to 8 of 8

Thread: jQuery, Ajax, WP, posting 4000B long text: 404 not found (2000B=OK)

  1. #1
    Join Date
    Nov 2011
    Location
    Rome
    Posts
    5

    Unhappy jQuery, Ajax, WP, posting 4000B long text: 404 not found (2000B=OK)

    Hello guys

    It's my first post/thread here, I haven't been active in forums for some time cause frankly I didn't need it, but this one just got me.
    I tried everything in my knowledge -and I mean everything, I'm a fanatic trial-and-error tester.
    Situation:
    jQuery, posts some data through Ajax collected primarily from a $('#textarea') to a php file on server.
    If the data is >X Bytes, I get 404 Not Found and the 404 Html page in Ajax error handler.
    Otherwise, everything runs smoothly and I get my results.

    I must specify that I post to a php file that loads Wordpress's wp-blog-header.php before doing calculations, as the code is intended to be part of a WPplugin.

    I tried to return header("HTTP/1.1 200 OK") as some suggested, considering WP was altering the header for not finding the file as part of the system, and it solved the issue locally for some cases, but not remotly.

    I tried to set the Content-Length in ajax call.
    I switched between the WP type of handling Ajax calls, and having my own .php file, in the folder of my plugin.

    Nothing worked, but trimming content.

    What could this be? post_data_size is 8M on server.
    Could this be maybe related to SSL as I've seen in some cases?
    I'm lost here.
    Thanks for your help,

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    I don't think it has anything to do with the length of the text, 4000b is less than 4Kb - if you can't post 4Kb through an ajax call then there's something wrong with the whole system.

    I've got my money on the fact that you're including the blog-header and it's spitting out the 404 error.

    Hard to say without seeing the PHP and it'd probably still be hard to say without seeing the code and a testing environment.

    All I can advise though, is that if you need to include the blog header in a PHP file, then you're better off doing AJAX the way WordPress intended - by using the wp_ajax() function and setting the URL of your request to wp-admin/admin-ajax.php

    It is weird, however, why it is that the different lengths trigger different errors.
    Last edited by aj_nsc; 11-15-2011 at 01:42 PM.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Nov 2011
    Location
    Rome
    Posts
    5

    Question

    I assure you: in my ajax file I tried with wp-config, now I left it with wp-load cause I thought it's somewhere in the middle, the way I read was that wp-blog-header.php calls wp-load, which calls in the end wp-config.
    But either way, it returns with ajax error 404 AND instead of results, brings back the 404 page (if the results would have been calculated anyway, I would have forced a header(200) and that's it, but they arent't, it's the 404 page).

    I agre with the admin-ajax.php situation, however if I call my own ajax file I could disable mod_security at the level of my plugin with an .htaccess file in the plugin folder, otherwise I don't have access to the wp-admin folder and so I couldn't help users easily in case of another infamous kind of error: Mrs. 403 .

    Your last line is what surprises me also: how the heck can it work with 2000B, but not 3000B... BYTES... no mega, no kilo, just bytes (.

    What would you need from the testing environment? Is this enough?

    Code:
        './configure' '--disable-mbregex' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-force-cgi-redirect' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-sockets' '--enable-zip' '--prefix=/usr' '--with-curl=/opt/curlssl/' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-pic' '--with-png-dir=/usr' '--with-sqlite=shared' '--with-ttf' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr' '--without-iconv'
    
        Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
    Here's the actual receiver, the php code, nothing fancy:
    Code:
    <?php
        
        require_once('../../../wp-load.php');
        
        //require('../../../wp-blog-header.php');
        //header("HTTP/1.1 200 OK");	//this line is required to avoid 404 error
        include('include/htmltools.php');
        
        if (isset($_POST['html']))
        	$s = $_POST['html'];
        else {
        	$s = '';
        }
        
        if ( !wp_verify_nonce( $_POST['nonce'], 'kwd-nonce' ) ) {
        	echo 0;
        	die(0);
        }
        global $wpdb, $kwd;
        
        switch (@$_POST['action']) {
        	case 'update-settings':
        		ajax_update_settings();
        		break;
        	default: //'get_kwd_tables':
        		ajax_get_kwd_tables();
        		break;
        }
        die();
        
        
        /* ===================================== */
        function ajax_update_settings() {
        	global $wpdb, $kwd;
        
        	if (isset($_POST['exclusion_list']))
        		if (get_magic_quotes_gpc())
        			$newOptions['exclusion_list'] = stripslashes($_POST['exclusion_list']);
        		else
        			$newOptions['exclusion_list'] = $_POST['exclusion_list'];
        	
        	if (isset($_POST['exclude_length_min']))
        		$newOptions['exclude_length_min'] = $_POST['exclude_length_min'];
        
        	$options = $kwd->get_options($newOptions);	//save Ajax posted options from Settings tab
        	echo 1;
        }
        
        
        /* =================================== */
        function ajax_get_kwd_tables() {
        	global $wpdb, $kwd; // this is how you get access to the database
        
        	require_once ('include/htmltools.php');
        
        	$options = $kwd->get_options();
        	$ht = new htmlTools($_POST['html']);
        
        	$ht->set('exclusionList', $options['exclusion_list']);
        	$ht->set('excludeLengthMin', $options['exclude_length_min']);
        
        	$ht->extractWords(array(1,2,3,4));
        	$ht->calculateDensity(2,0);
        	$out['tab11'] = $ht->display();
        
        	header("Content-Type: application/json; charset=utf-8");
        	$x = json_encode($out); //$json->encode($out);
        	echo $x;
        	die();	
        }
        
        ?>

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    What about the code for the XHR?
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Nov 2011
    Location
    Rome
    Posts
    5
    What is the XHR code?
    This is the Javascript used to call the ajax page (I also tried with "xhr.setRequestHeader("Content-length", 0) and got the "Refused to set unsafe header "Content-length" in the console, and same 404 error):
    Code:
    var data1 = {
        action: 'get-tables',
        html: $('#content').val(),
        nonce: kwd_settings.nonce
    };
    $.ajax({
        url: kwd_settings.ajax_url,
        type: 'POST',
        data: data1,
        scriptCharset: 'utf-8',
        timeout: 10000, /*10 seconds*/
        beforeSend: function( xhr ) {
            xhr.setRequestHeader("Content-length", $('#content').val().length+500);
            showProgress(true);
        },
        error: function (request, ajaxOptions, thrownError) {
            alert(request.statusText);
            alert(request.responseText);
            showProgress(false);
        },
        success: function( response ) {
            var r= response;
            showResponse(r);
        }
    });

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Why are you setting the content length header before sending? If this is jQuery, and it looks like it is, it's not required, jQuery does all this for you.

    Does it make a difference if you get rid of this beforeSend method?
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  7. #7
    Join Date
    Nov 2011
    Location
    Rome
    Posts
    5
    No, I added the beforeSend because I was desperate, and because I saw that the big influencer was the length of content. So I thought in my innocent mind that maybe, if I would tell it the size of content, something could change.

    Yet, nothing changes... I will try to close this blog, and do more tests, although this is already wasting a lot of nervous energy on my part I'm out of solutions...

  8. #8
    Join Date
    Nov 2011
    Location
    Rome
    Posts
    5
    Same for scriptCharset: 'utf-8' and timeout: 10000, /*10 seconds*/
    I added these in "desperation mode", otherwise it works very well without them.
    What this plugin does is grab the code from the wordpress content textarea, and send it to a php script to do some analysis on it. Nothing incredible. Yet...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles