I struggle to make this AJAX to work URL WORKS GET from browser

I struggle to make this work …
START and END ALERTS FIRED but non SUCCESS OR ERROR ALERTS FIRED NEITHER CONSOLE RESULTS… any hint…? URL WORKS GET from browser… PARTICULARLY I PURCHASED A TEMPLATE AND NEEDED TO MAKE MAKE MODEL DYNAMIC THE: // I STARTED FROM MODEL OF CAR

function fetchDataForCarModel() {
	alert("START");
		$.ajax({
			url: URL, // THIS WORKS GET from browser
			type: 'POST',
			data: {
			 'fn': 'bci_model',  
                         'makeId': 35
			},
			async: true,
			dataType: 'json',
			success: function(results) {
            	        alert("success");
			console.log(results);
                        row = results.dataResult;
                        $("#modelId").html("");
            
				var i,
					len = results.dataResult.length,
					row, list=$("select#modelId"), items = []; 

				if (len > 0 ) {
					                    
                    for (i = 0; i < len; i += 1) {
						row = results.dataResult[i];
						items.push('<option value="'+row.id+'">'+row.name+'</option>');
					}
               
					list.html("<option value='' selected='selected'>Any Model</option>"+items.join("\n")+"");
                }
            }, 
			onerror: function(error) {
				alert("error");
				console.log(error);
			}
        });	
		alert("END");
}   //  END - fetchDataForCarModel()

I have in PHP
echo json_encode($data);

I tried again but if change dataType: ‘json’, to dataType: ‘text’, i get string, where you think is the error??

may the template has conflict scripts - but again only text works…

What HTTP headers are being returned?

GET in browser works… again dataType:json returns error but text return str that tried JSON.parse() but failed…

below the headers? may is a conflicts script inside template…?

See the
Response Headers
Content-Type: text/html

That is why asking for text works.

If you want to ask for JSON or XML the data should be returned as that Content-Type
application/xml
application/json

Content type in response
I have to set in jQ or in PHP…? How in PHP?

How above the request is json and response text…?

header('Content-Type: application/json');

I used this in PHP - i got json in response but neither alert success nor console.log results…

any hint…

header(‘Content-Type: application/json’);

Are you getting alert/console.log for the onerror?

sometimes error alert getting - i did many revisions

echo json_encode($data); // this must followed by die(); // for same site ajax request???

I tested js/jQ script isolated - outside the template… but neither gives results…

I get results ONLY on get request in address bar of browser … but Not GET/POST from JQ/PHP… response headers are the same OK as above…

any hint


Array
(
    [dataAvailable] => 1
    [dataResult] => Array

ins it char encoding of php or db mysql… ^^^ this getting on echo “

”.print_r($data).“
”;
well? what are these: …?

I run success an ajax php call like
$data = array(“1”=>“77”, “gh”=>“8”, “kk”=>array(“kk”=>“kk”, “jj”=>“kk”));

	  echo json_encode($data); 

	  die();

Ah, I never considered that might be the problem here.

That’s a character encoding issue. Usually from not saving the file as UTF-8 without BOM

The BOM characters can not typically be seen with human eyes. But they can cause “header already sent” and “illegal character” errors.

Make sure your text editor settings are correct.

how set utf-8 in php file handles ajax requests from html pages…?

It depends. What are you using to create / edit your files

I have UTF-8… Is it encoding problem…? or…

see my Dreamweaver cs6::

Again in GET in browser getting data but in GET or POST AJAX NOTHING…

Any other hint…??? what change encoding to???

Hmm. I’m not familiar with DreamWeaver, but the default settings look correct - UTF-8, and BOM is not included.

Yet the  is definitely an encoding issue.

I notice the dialog says “New Documents”, does it also have some sort of “convert encoding to” pane for not-new documents?

Yes you refer isn’t checkbox below encoding drop down…

What other encoding to try…?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.